Solved

cube root of x

Posted on 1997-11-09
6
1,007 Views
Last Modified: 2012-05-04
Assignment: Write a program to read in a real number x and output the integer n closest to the cube root of x. Assume that x is always non-negative. The program should allow the user to repeat this calculation as often as desired.

We just covered functions, loops (not yet at arrays). Please provide the code. Thanks!
0
Comment
Question by:chadd082197
6 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1216617
function cube(x:real):integer;
begin
    x=x*x*x;
    cube=round(x);
end

Hope this helps.  
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1216618
kellyjj, I believe chadd is looking for a function that will return the cube root of x, not the cube of x

Chadd, I could give you the code but it goes against my principle of completing another persons assignment. This way you won't learn anything. If you wish, I could give you the pseudo-code based on the Newton-Raphson method. You could then try to implement it & if you face any problem you can ask me more specific questions.

If you are still interested only in the code, I hope someone else can help you

Wilfred

0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1216619
wpinto :  

Opps!! I guess I don't know how to read. hehe  =]   Darn it.  Oh well, it will teach chaddd to get others to do his work.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:chadd082197
ID: 1216620
To kellyjj: This will help me very little. And I am not getting others to do my work, I simply wanted a little help (I thought that's what this site was all about). Thanks anyway tho.

To wpinto: I respect that. Any algorithm that you could provide would be helpful I'm sure. The 200 points is still at large.
0
 

Accepted Solution

by:
mwdejager earned 200 total points
ID: 1216621
This fuction assumes that x >= 0.
If a^n = e^(n log a)  where the base is e,
then the following function should work.

Function CubeRoot(x : Real) : Real;

Begin
      If x = 0 Then      
            CubeRoot := 0
      Else
            CubeRoot := Exp(Ln(x) / 3);
End;
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1216622
Hi chadd,

The above code will give you the cube root if you have access to functions such as log & exp. Also note that cube root of x is equivalent to x^(1/3), so if you have access to a power function its even easier.

The below algoritm needs only access to the addition & division operators

Here's the algorithm I promised

To compute y = x^(1/3)    
y^3 = x    
y^3-x = 0    
Let f(y) = y^3-x
f'(y) = 3y^2    
By Newton iteration:
y[n+1] = y[n] - f(y[n])/f'(y[n]) -------- f'(y[n]) <> 0
y[n+1] = y[n] - (y[n]^3-x)/(3y[n]^2) ---- 3y[n]^2 <> 0
y[n+1] = y[n] - (y[n]-x/y[n]^2)/3 ------- y[n] <> 0
y[n+1] = (2y[n] + x/y[n]^2) / 3 --------- x <> 0    

Now the answer is in y[n] if y[n+1] = y[n]. Since we are dealing with floating point nos, its highly unlikely that you will reach the above condition. So you will have to check for a small difference between them eg. y[n+1] - y[n] < 0.000001. Note, the smaller the constant, the more precise the answer but the number of iterations will be more!

Here's the pseudo code

Function CubeRoot(x : Real) : Real;
VAR Yn : Real;
VAR Yn1 : Real;
Begin
   If x = 0 Then
   CubeRoot := 0
Else
   Loop
   {
     Yn1 = ((Yn + Yn) + x / Yn / Yn) / 3;
     if (absolute(Yn1 - Yn) < 0.00001)
     exit loop;

     Yn = Yn1;
   }
   CubeRoot := Yn1;
End;

Hope this helps

Wilfred
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question