Both ASP and PHP have a function that allows you to calculate the square root of a number. What if we wanted the cubic root of a number, or some deeper root? Calculating the root of a number is the same as raising that number to a fractional exponent.
ASP
function root(x, y)
root = x ^ (1 / y)
end function
PHP
function root($x, $y)
{
return pow($x, 1/$y);
}
- x
- the number you want the root of
- y
- the depth you want to go (2 = square, 3 = cubic, etc.)
2 comments:
No idea about ASP, but your PHP example is wrong. ^ is the bitwise XOR operator in PHP.
This is what you want:
public function nth_root($x, $y)
{
return pow($x, 1/$y);
}
Thanks, I've updated the example.
Post a Comment