Calculating the nth root of a number

P

Protoman

How do I write a function to calculate the nth root of a number; I'm
sick of using pow() with a fractional exponent to do the job. Got any
ideas on how to do this? Thanks for the help!!!
 
P

Protoman

I'm trying to do this as academic exercise; could you tell me how to
implement the divide and average method in code? Thanks for the help!!!
 
T

tmartsum

1) Your questions is (though intereressing) way of topic (C++)
2) And why dont you seach the internet first before asking ?

Google gives good results on something like
+square root +cubic root +nth root +algorithms

And by the way - do you ever complete a task ? (Eg your PI calculations
- can I see the source ?)

/Thorbjørn
 
K

Kai-Uwe Bux

Protoman said:
I'm trying to do this as academic exercise; could you tell me how to
implement the divide and average method in code? Thanks for the help!!!

What about starting with square roots?

Here the idea is that you generate a sequence x_1, x_2, x_3, ... of
approximations that become better and better. The formula is:


x_1 = A

x_{i+1} = 1/2 ( x_i + A / x_i );


A first draft of some experimental code would thus look like this:

#include <iostream>
#include <boost/lexical_cast.hpp>

double recurse ( double x, double A ) {
return( 0.5 * ( x + A/x ) );
}


int main ( unsigned argn, char * args[] ) {
if ( argn >= 1 ) {
double A = boost::lexical_cast< double >( args[1] );
double x = A;
for ( unsigned i = 0; i < 20; ++i ) {
std::cout << x << '\n';
x = recurse( x, A );
}
}
}


Now, there are some problems for you to solve:

a) write a function

double square_root( double A ) {
...
}

based on this idea.

b) In this function, you need to decide how many iterations you want. In the
sample code, I just hardcoded 20. But that is stupid: if you play around
with different values, you will find that sometimes the routine converges
faster and sometimes it takes longer. So, you might want to come up with a
dynamic criterion for when to stop.


Once you understand the square root thing, the n-th root stuff will be a lot
easier.



Hope this helps

Kai-Uwe Bux
 
P

Protoman

I'll try that; I just don't have boost. I did finish it; i used
atan(1.0) to do it:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

long double pi(void){return (4*atan(1.0));}

int main()
{
cout << "Pi: " << pi() << endl;
system("PAUSE");
return 0;
}

I don't have boost, so could you rewrite it?
 
K

Kai-Uwe Bux

Protoman said:
I'll try that; I just don't have boost.

a) When you use a pronoun like "that" it would help if you had a quote
before, so that I know what you are talking about. You should not assume
that other peoples news-reader look like anything you are used to.

b) Get boost. It's free.

I did finish it; i used atan(1.0) to do it:

"it" ?

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

long double pi(void){return (4*atan(1.0));}

int main()
{
cout << "Pi: " << pi() << endl;
system("PAUSE");
return 0;
}

I don't have boost, so could you rewrite it?

Rewrite this piece of code to do what? Please be a little more specific. I
apologize, but I honestly have no idea what you are talking about. [And I
suspect, it's not my fault.]


Best

Kai-Uwe Bux
 
T

tmartsum

replace
double A = boost::lexical_cast< double >( args[1] );

with something like
std::cin >> A;

and your homework should be allmost done
(Haven't looked at the algoritm)

Now your piece of code do calculate PI - nut with what accuracy.
Not bigger than long double (and atan) has.

You wanted it with 100.000+ digits .....
 
T

tmartsum

There must be a restriction to this algoritm !?
A division by zero can happen if x==0 .......
 
K

Kai-Uwe Bux

tmartsum said:
replace
double A = boost::lexical_cast< double >( args[1] );

with something like
std::cin >> A;

and your homework should be allmost done
(Haven't looked at the algoritm)

Now your piece of code do calculate PI - nut with what accuracy.
Not bigger than long double (and atan) has.

You wanted it with 100.000+ digits .....

Hi,


did you actually post this as a reply to my post? If so, who do you think
the word "you" in your post refers to?


Best

Kai-Uwe Bux
 
T

tmartsum

The post should of course has been a reply to Protoman - who I am
refering to as "you".
My mistake - sorry.

/Thorbjørn
 
K

Kai-Uwe Bux

Protoman said:
To make it compilable w/o boost.

I am glad to see that you actually started using this forum to investigate
C++, and I hope you are having a good experience. However, your postings
still leave room for improvement:


When you refer to something, QUOTE IT!


All I have on my screen is that single line of yours. And it yields:
unresolved symbol error (reference to "it" undefined).

You need to keep in mind that usegroups work different than chat rooms. It
is completely unpredictable to you and me which of the news are still
hosted on the server I am using. Never presuppose that I can access easily
anything you wrote previously.


Best

Kai-Uwe Bux
 
P

Protoman

Well, I use google groups, which sadly, you can't use a newsreader. I'm
going to search on google for an algorithm; I'll report what I find.
 
A

Alf P. Steinbach

* Protoman:
Well, I use google groups, which sadly, you can't use a newsreader.

First, it's possible to quote properly even using Gaggle. You have to click
on some things first. That's all.

Second, of course you can use a newsreader.

Newsreaders are free, and so are news-servers.

I'm
going to search on google for an algorithm; I'll report what I find.

Good idea, but you may just visit your nearest university library and walk
home with the three volumes of Knuth's "The Art of Computer Programming" in
your appropriately sized rucksack or bag (they're heavy, also physically!).

XFUT: [comp.programming]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top