Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

getting a cube root

by Anonymous Monk
on Oct 08, 2003 at 18:51 UTC ( [id://297695]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to find how to get a cube root - all I can find is:

sqrt EXPR - This function returns the square root of EXPR. If EXPR is omitted, it returns the square root of $_. For other roots such as cube roots, you can use the ** operator to raise something to a fractional power.

which is great, except that I don't know what it means!

Replies are listed 'Best First'.
Re: getting a cube root
by Ovid (Cardinal) on Oct 08, 2003 at 18:55 UTC

    The Nth root of a given number is that number raised to the reciprocal of N. Thus:

    print $num ** (1/3);

    Update: Heh. Looks like I beat the rush. Is this where I delete everything and replace it with "first post!"? :)

    Cheers,
    Ovid

    New address of my CGI Course.

Re: getting a cube root
by asarih (Hermit) on Oct 08, 2003 at 19:25 UTC
    All the replies posted so far finds a real root. Is there a method to find complex roots as well? Math::Complex doesn't seem to cut it.

    Update: As pointed out below, c=r^(1/n)*e^(2*pi*i*j/n) (for j=0,1,...,n-1) will do the trick, but I was thinking someone should have written an easy access to the roots, as in:

    @roots=complex_roots($real, $n)
    to give all the n-th roots of $real. Hmmm.... This shouldn't be too difficult. I'll leave it as exercise to the reader. :)
      You can use trig and brute force a method -- I'm a little busy to give it a go, but the complex roots lie on a circle in the complex plane of the same radius as the real root. With a cube root, that means you need to find the coords of the points at 120 and 240 degrees (1/3 and 2/3 around the circle) on that circle -- hopefully that's clear. For the nth root you need to look at the (i * 360/n) degree points with i = 0..(n-1). i=0 corresponds to the real root.

      Update:

      #!/usr/bin/perl -w use strict; my $twoPi = 4 * atan2(1, 0); for (2..5) { my @roots = nthRoots(8, $_); print "$_ th roots of 8: $/"; foreach my $root (@roots) { print "$root->{Real} $root->{Imag}$/"; } }; sub nthRoots { my $x = shift; my $n = shift; # Should check for integer powers my @roots; $roots[0] = { Real => $x ** (1/$n), Imag => 0 }; for (1..($n-1)) { push @roots, { Real => $roots[0]->{Real} * cos( $_ * $twoPi/$n ), Imag => $roots[0]->{Real} * sin( $_ * $twoPi/$n ) }; }; return @roots; }
Re: getting a cube root
by ChrisS (Monk) on Oct 08, 2003 at 18:55 UTC
    To find the cube root, raise a number to the 1/3 power.
    print 8 ** (1/3); # prints 2
Re: getting a cube root
by BUU (Prior) on Oct 08, 2003 at 18:55 UTC
    27**(1/3)
Re: getting a cube root
by Jaap (Curate) on Oct 08, 2003 at 18:56 UTC
    It means this:
    8**(1/3) == 81/3 == cube root of 8 == 2

      small math lesson for AM: just incase you didn't realize, taking the nth root of anything is the same as raising that expression/number/etc..etc..etc... to the 1/n power where n is the root you are trying to find.

      to find the 5th root of x, you simply do x**(1/5), 218th root x**(1/218), you get the picture. FYI: very much respect for the question, but just not a perl way of doing it.. just a math way. You can write your own sub to do it if you really wanna make it readable for newbies or non-coders.

      #!/perl use strict; #Take the 3rd root of 8; my $root = nthRoot(8,3); print $root; #Just pull the parameters off the #@_ array as they are needed #in the return statement sub nthRoot {return (shift)**(1/shift);}
Re: getting a cube root
by svsingh (Priest) on Oct 09, 2003 at 01:03 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://297695]
Approved by Ovid
Front-paged by DrHyde
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found