Nth root with only + - * and /

I have to write a program to find any root of any number using only +, -, /, and *. The root should be the exact root or as close as 0.0000001 or -0.0000001. Here is what i came up with so far, but this doesn't work for all roots. If anyone could please improve it so it can produce the desired results, i will greatly appreciate it.
Thanks.
(you can email me at mustafacenter@ioip.com)

Here is the code:
{
double x = 0;
int y = 0;
double z = 0;

cout<<"enter x"<<endl;
cin>>x;
cout<<"enter y"<<endl;
cin>>y;
double max = x;
double min = 1;
int flag = 0;
double guess = 0;
while(flag != 1)
{
z = (min+max)/2;
guess = z;
cout << "guess = " << guess;
for (int i = 0; i<y; i++)
{
guess = guess*z;
}
cout << "\nResult = " << guess;
if(((guess - x) >= -0.0000001) && ((guess - x) <= 0.0000001))
{
flag = 1;
cout << "\nThe " << y << "th root of " << x << " is: " << z << endl;
}
else
if (guess > x)
{
cout << "\nGuess is too big, changing: " << z << endl;
max = z;
}

else
{
cout << "\nGuess is too small, changing: " << z << endl;
min = z;
}
cout << "\nFlag = " << flag << endl;
}
}
[1776 byte] By [ziajan] at [2007-11-17 22:39:33]
# 1 Re: Nth root with only + - * and /
What's the point of homework if you get someone else to do it ? ;)

P.S. Use descriptive titles. "Help the novice" does not tell anyone what your question is about.
Yves M at 2007-11-10 8:41:40 >
# 2 Re: Nth root with only + - * and /
well, what you'd basically want to do is use a Taylor or McLaurin series for this.

If i'm not mistaking, this is the same thing that your FPU does.

in a quick look at the net i found this:

http://www.jtaylor1142001.net/calcjat/Solutions/Series/Taylor/taylorsqrt.htm

Hope this helps,

Steven Roelants
s. roelants at 2007-11-10 8:42:40 >
# 3 Re: Nth root with only + - * and /
well, i am sorry i tried my best. i just need some help. i don't want anyone to do the entire thing for me. maybe there is something wrong with the method i am using or whatever, but i just need a little jump.:confused:
ziajan at 2007-11-10 8:43:46 >
# 4 Re: Nth root with only + - * and /
Originally posted by s. roelants
well, what you'd basically want to do is use a Taylor or McLaurin series for this.

If i'm not mistaking, this is the same thing that your FPU does.

in a quick look at the net i found this:

http://www.jtaylor1142001.net/calcjat/Solutions/Series/Taylor/taylorsqrt.htm

Hope this helps,

Steven Roelants

thank you so much for your reply, but i am looking for any root of any real number.
ziajan at 2007-11-10 8:44:41 >
# 5 Re: Nth root with only + - * and /
Do you know how to do a square root with pen and paper?

Then you only have to imitate the procedure an code it, you only will need + - and *
Doctor Luz at 2007-11-10 8:45:51 >
# 6 Re: Nth root with only + - * and /
Sure OK,

So what does not work in your program ?

By the way, if you past code, use the [code] and [/code] tags and check my signature for doing some syntax highlighting. It's nicer to read.
Yves M at 2007-11-10 8:46:46 >
# 7 Re: Nth root with only + - * and /
Originally posted by Doctor Luz
Do you know how to do a square root with pen and paper?

Then you only have to imitate the procedure an code it, you only will need + - and *

How do you do an nth root using only pen and paper ? :confused:
Yves M at 2007-11-10 8:47:50 >
# 8 Re: Nth root with only + - * and /
Originally posted by Doctor Luz
Do you know how to do a square root with pen and paper?

Then you only have to imitate the procedure an code it, you only will need + - and *

Thanks Doc, but it's not square root i am looking for. it's any root of any real number other than 1 and -1.
ziajan at 2007-11-10 8:48:53 >
# 9 Re: Nth root with only + - * and /
Aha, so you want to be able to get 15^(1/1.5) for example, no ?
Yves M at 2007-11-10 8:49:52 >
# 10 Re: Nth root with only + - * and /
Ok, since it's only a small bug, here is a corrected version:

int main()
{
double x = 0;
int y = 0;
double z = 0;

cout<<"enter x"<<endl;
cin>>x;
cout<<"enter y"<<endl;
cin>>y;

double max = x;
double min = 1;
int flag = 0;
double guess = 0;

while(flag != 1)
{
z = (min+max)/2;
guess = z;
cout << "guess = " << guess;
for (int i = 1; i < y; i++)
{
guess = guess * z;
}
cout << "\nResult = " << guess;

if(((guess - x) >= -0.0000001) && ((guess - x) <= 0.0000001))
{
flag = 1;
cout << "\nThe " << y << "th root of " << x << " is: " << z << endl;
} else if (guess > x) {
cout << "\nGuess is too big, changing: " << z << endl;
max = z;
} else {
cout << "\nGuess is too small, changing: " << z << endl;
min = z;
}
cout << "\nFlag = " << flag << endl;
}
return 0;
}
Yves M at 2007-11-10 8:50:53 >
# 11 Re: Nth root with only + - * and /
Originally posted by ziajan
I have to write a program to find any root of any number using only +, -, /, and *. The root should be the exact root or as close as 0.0000001 or -0.0000001.
I think this (http://home.intersrv.com/~dcross/nth-root.jpg) is what you're looking for.

It's better if you know calculus because you can understand it, but even if you don't the method still works.

Basically, you have an initial guess and you have a recursive calculation calculation that will add to the guess until it's within a specified range.
SolarFlare at 2007-11-10 8:51:47 >
# 12 Re: Nth root with only + - * and /
lol did you just write and scan that, solarflare ? ;)
Yves M at 2007-11-10 8:52:58 >
# 13 Re: Nth root with only + - * and /
Originally posted by Yves M
lol did you just write and scan that, solarflare ? ;)
No, I had seen that site before and remembered it. But I could if I wanted to :D.
SolarFlare at 2007-11-10 8:53:53 >
# 14 Re: Nth root with only + - * and /
Originally posted by Yves M
Ok, since it's only a small bug, here is a corrected version:

int main()
{
double x = 0;
int y = 0;
double z = 0;

cout<<"enter x"<<endl;
cin>>x;
cout<<"enter y"<<endl;
cin>>y;

double max = x;
double min = 1;
int flag = 0;
double guess = 0;

while(flag != 1)
{
z = (min+max)/2;
guess = z;
cout << "guess = " << guess;
for (int i = 1; i < y; i++)
{
guess = guess * z;
}
cout << "\nResult = " << guess;

if(((guess - x) >= -0.0000001) && ((guess - x) <= 0.0000001))
{
flag = 1;
cout << "\nThe " << y << "th root of " << x << " is: " << z << endl;
} else if (guess > x) {
cout << "\nGuess is too big, changing: " << z << endl;
max = z;
} else {
cout << "\nGuess is too small, changing: " << z << endl;
min = z;
}
cout << "\nFlag = " << flag << endl;
}
return 0;
}

can't believe i made such a silly mistake. but thank you so much. i could have never done it on my own.
ziajan at 2007-11-10 8:54:59 >
# 15 Re: Nth root with only + - * and /
Originally posted by ziajan
can't believe i made such a silly mistake. but thank you so much. i could have never done it on my own.
Now that is where I don't believe you ;) Seriously, when you get strange results, the best thing is to go through the algorithm on paper for some values. Also write down the values your program should compute. Then debug your program and on the first thing that's off, you will be like "AHA!" and you'll find the bug ;)
Yves M at 2007-11-10 8:55:59 >
# 16 Re: Nth root with only + - * and /
since i wasn't really helpfull with my post i looked up the mclaurin series for a^x
maybe it can be of some use :D

mclaurin of a^x

a^x=e^(x*ln(a))

lets set z to x*ln(a) for ease of notation or calc
then
a^x=1+z/1!+(z^2)/2!+(z^3)/3!+(z^4)/4!+...
with
n! being 1*2*3*...*n

of course we still don't know ln(a)

mclaurin for ln(a)
for a>0 this would result in:
lets set w to ((a-1)/a) for ease of notation
then
ln(a)=2*z+(2/3)*z^3+(2/5)*z^5+(2/7)*z^7+...

i realize that you still have two series to take into calculate so it's not very usefull
s. roelants at 2007-11-10 8:56:53 >
# 17 Re: Nth root with only + - * and /
Mclaurin seems to be causing a lot of trouble around these parts - why don't you just hire a hitman instead of writing the program? It's definitely the easier of the two possibilities.
SolarFlare at 2007-11-10 8:58:01 >
# 18 Re: Nth root with only + - * and /
I agree with SolarFlare. I never liked the guy (too much of a trouble maker -- and besides, he's trying to steal Taylor's limelight). But seriously, don't use him for this problem. The radius of convergence will make him all but useless, particularly since there are functional iterations that will accomplish this over the entire positive reals. But this seems like a moot point now since Yves has worked so hard to get the original poster's question solved... :)
galathaea at 2007-11-10 8:59:02 >
# 19 Re: Nth root with only + - * and /
Originally posted by galathaea
... Yves has worked so hard to get the original poster's question solved... :)
Well yes, I had to format the code correctly, tha's a lot of work :rolleyes:
Yves M at 2007-11-10 9:00:00 >