I am trying to make a power function to calculate the power of 17 ^ 2147482999. I tried this code:
function ipow($a, $b) {
if ($b<0) {
echo "B must be a positive integer";
}
if ($b==0) return 1;
if ($a==0) return 0;
if ($b%2==0) {
return ipow($a*$a, $b/2);
} else if ($b%2==1) {
return $a*ipow($a*$a,$b/2);
}
return 0;
}
Function call:
echo ipow($a, $b);
Error:
Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\spoj\LASTDIG.php on line 23
Is there any other way to calculate power for such large values? A built-in function pow()provides output INF.
UPDATE
If it seems impossible to get the whole answer, is it possible to extract at least the last 5-10 digits of the answer by some mathematical approach?
source
share