473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Calculating the nth root of a number

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!!!

Sep 16 '05 #1
14 20933
pow() is as simple as it gets. What do you want?

Sep 16 '05 #2
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!!!

Sep 16 '05 #3
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

Sep 16 '05 #4
Protoman wrote:
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
Sep 16 '05 #5
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?

Sep 16 '05 #6
Protoman wrote:
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
Sep 16 '05 #7
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 .....

Sep 16 '05 #8
There must be a restriction to this algoritm !?
A division by zero can happen if x==0 .......

Sep 16 '05 #9
tmartsum wrote:
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
Sep 16 '05 #10
The post should of course has been a reply to Protoman - who I am
refering to as "you".
My mistake - sorry.

/Thorbjørn

Sep 16 '05 #11
To make it compilable w/o boost.

Sep 17 '05 #12
Protoman wrote:
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
Sep 17 '05 #13
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.

Sep 17 '05 #14
* 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]

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
4
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
13
by: Kishor | last post by:
Hi Friends Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32 Output : 5th root of 32 is 2 Ex:(1) Input : 243 Output : 5th root of 243 is...
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
3
by: Ron Vecchi | last post by:
I need to calculate the age of a person based on a DateTime BirthDate I was thinking TimeSpan ts = DateTime.Now - BirthDate; //I can get the days but not years. // I could check each...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
10
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
1
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.