For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Math Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Math Question
Sanbuah@aol.com

2006-01-30, 6:56 pm

I know that Perl has a function to determine the square root of a number.
For example, to determine the square root of 121 code "sqrt(121)". Is there a
built-in Perl function that enables one to determine the nth root of a number
where n is a number greater than 2. For example, is there a function that
enables me to determine that the 4th root of 81 is 3?

Thanks,

Frank

Tom Phoenix

2006-01-30, 6:56 pm

On 1/30/06, Sanbuah@aol.com <Sanbuah@aol.com> wrote:

> I know that Perl has a function to determine the square root of a number.
> For example, to determine the square root of 121 code "sqrt(121)". Is the=

re a
> built-in Perl function that enables one to determine the nth root of a n=

umber
> where n is a number greater than 2. For example, is there a function tha=

t
> enables me to determine that the 4th root of 81 is 3?


Mathematicians have more to say about it, but the Nth root is the same
(for your purposes) as the 1/Nth power:

print "The fourth root of 81 is ", 81 ** (1/4), ".\n";

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
davidfilmer@hotmail.com

2006-01-30, 6:56 pm

Sanbuah@aol.com wrote:
> For example, is there a function that
> enables me to determine that the 4th root of 81 is 3?


Are you aware that taking the fourth-root of a number is the same thing
as raising the number to the one-fourth power?

Try:

perl -e "print 81**.25"

--
http://DavidFilmer.com

Paul Lalli

2006-01-30, 6:56 pm

Sanb...@aol.com wrote:
> Is there a
> built-in Perl function that enables one to determine the nth root of a number
> where n is a number greater than 2. For example, is there a function that
> enables me to determine that the 4th root of 81 is 3?


There's no function, but there is an operator. To figure out what it
is, remember that "the N'th root of X" is the same thing as saying "X
to the power of (1 over N)":

my $nth_root = 81**(1/4);

If you really wanted to use a subroutine to do it for you, it's not
hard to write:
sub nth_root {
my ($n, $x) = @_;
return $x ** (1/$n);
}

print "4th root of 81 is: ", nth_root(4, 81), "\n";

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com