# Thread: Finding root of a number

1. ## Finding root of a number

So i'm trying to write a program that finds the root of a number without formulas i.e. sqrt(x).

so far i have come up with
Code:
```void Roots(double a, int b)
{
double x;

switch(b)
{
case 2:
for(x = 0; x < a; x = x + .001)
{
if ( a == x*x)
{
cout << "\n\tThe square root of " << a << " equals " << x;
}
}
break;
default:
cout << "\n\tI haven't got to that switch yet";

}
}```
and in my calling program i have:
Code:
`Root(5,2);`
When i change that 2 to a 3, i get my default, but when i have it as a 2 to activate case 2, it prints nothing out. Why?

Also can anyone explain to me how you find rational roots of numbers i.e. 2^(5/2). I want to write a general function to do not only whole roots, but also rational roots. I have been reading this but i'm tired and its not clicking : Calculate square root without a calculator

2. You could get rid of the switch if you wrote your own Pow function.

Code:
`if ( a == Pow(x, b) )`
The next thing is that it is not always going to be exactly equal, since your function has poor precision (and floating point numbers are only approximations anyway). You should rather try to minimize the difference between a and the Pow.

Also, instead of increments of 0.001, you might also try something like a binary search. Start with a gross overestimation (e.g a) and an underestimation (e.g 0), and test the middle point between those. Repeat until result is good enough.

3. i can't use pow or sqrt or the like, defeats the purpose.

i'm trying to do it using only for loops and if commands.

Also why doesn't it return a thing for 2?

4. look up Newton's Method.

5. I looked up the newtons method and it works for when you know the root and you can make switch for it, which was what i was doing at first. But i can't do it for an unknown fractional root i.e. (3/7) because that would entail putting a pow in it right? this is what i mean

Code:
```		case 4:
for (c = 0; c  < 15; c++)
{
d = d - (d*d*d*d - a)/(4*d*d*d);
}
cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
break;```
i can only do those d like that hwen i know the root beforehand, otherwise i would need to do pow(d,b) which would entail using the pow and that defeats what i do. Got a suggestion?

6. Make your own function that will provide you the functionality of pow(number) and try using that.
Also, if you want to calculate root with rational numbers, yes you can, but make a class of rational numbers and overload some of necessary operators to make it work...

7. so i basically got my case ones down
using logic and newton method

Code:
```void Roots(double a, int b)
{
double x,d = 10;
int c;

switch(b)
{
case 2:

for(x = 0; x < a; x = x + .001)
{
if ( a / x >= x - 0.001 && a / x <= x + 0.001 )
{
cout << "\n\tThe square root of " << a << " = " << fixed << setprecision(3) << x;
}
}
break;
case 3:
if ( a > 0 )
{
for (c = 0; c  < 15; c++)
{
d = d - (d*d*d - a)/(4*d*d);
}
cout << "\n\tThe third root of " << a << " = " << fixed << setprecision(3) << d ;
}
else
{
a = a * -1;
for (c = 0; c  < 15; c++)
{
d = d - (d*d*d - a)/(4*d*d);
}
cout << "\n\tThe third root of " << a * -1 << " = " << fixed << setprecision(3) << d * -1;
}
break;
case 4:
for (c = 0; c  < 15; c++)
{
d = d - (d*d*d*d - a)/(4*d*d*d);
}
cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
break;
default:
cout << "\n\tYou didn't assign that root";
}
}```
but i haven't learned overloading yet, is it possible to use a logical iterator to calculate the nth root without using newton method?

oh a side note, i was practicing ways of doing it differently
Code:
```		case 4:
/*
for (c = 0; c  < 15; c++)
{
d = d - (d*d*d*d - a)/(4*d*d*d);
}
cout << "\n\tThe 4th root of " << a << " = " << fixed << setprecision(3) << d;
break;
*/
for (x = 0; x < a; x++)
{
if ( x * x * x * x <= a && (x + 1 ) * (x + 1)*(x + 1)*(x + 1) > a)
{
for ( y = 0; y < 1; y = y + .1)
{
x = x + y;
if ( x * x * x * x <= a && (x + .1 ) * (x + .1) * (x + .1) * (x + .1) > a)
{
for (z = 0; z < .1; z = z +.01)
{
x = x + z;
if ( x * x * x * x <= a && (x + .01 ) * (x + .01) * (x + .01) * (x + .01) > a)
{
for (Third = 0; Third < .01; Third = Third + .001)
{
x = x + Third;
if (x * x * x * x <= a && (x + .001 ) * (x + .001) * (x + .001) * (x + .001) > a)
{
cout << "\n\tThe 4th root of " << a << " = " << x;
}
}
}
}
}
}
}
}
break;```
This worked for perfect 4th roots like 81 and 16. But when its a number like 4 or 20 or etc.., it returns nothing. Why?

8. Listen what you are trying to do is hard code...
I suggest you something...
Take a copy and pen
Start thinking that if you get the same problem, how your mind will start solving this with all the constraints you are specified with...
Make a step by step english or your natural language statements, for your ease of understanding, and as soon as you are finished with solving with your mind, now simulate it with the programming language....
I guarantee you that you will solve this problem in no time...

9. The n'th root of a number can be found by dividing the logarithm of the number by n, as follows.

// cube root of 256
x = 256;
n = 3;
a = log(x) / n;
r = exp(a);

Plus, this works for fractional and negative powers.

Plus, you can use logs to any base, but natural logs (base e) are the most convenient.

If you can't use log and exp, then write your own, based on these polynomial series.
Taylor series - Wikipedia, the free encyclopedia