Power function in php to calculate 17 ^ 2147482999

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?

+3
source share
6 answers

You can use the bcpowmod function as follows:

<?php echo bcpowmod(17,2147482999,10000000000); ?>

the result is equal 8849802353, which means that 17 ^ 2147482999 mod 10000000000 or, the last 10 digits 17 ^ 2147482999 is 8849802353.

+3
source

PHP. , , 64- .

bcmath bcpow. ( , , gmp.)

 print bcpow(17, 2147482999);
+4

1e + 2642368139, , . , :

17^2147482999 = 10^(log(17^2147482999))
    = 10^(2147482999 * log(17))
    = 10^(2147482999 * 1.23045)
    = 10^(2642368139.79773)
    = 10^2642368139 * 10^0.79773
    = 6.27669e+2642368139
+3

I suggest you learn BigInteger , the constant PHP_INT_MAX will tell you how big your platform can handle. In 64 bits, this returns 9223372036854775807, which is far from your result in decimal notation.

0
source

Try changing the algorithm and instead of working with numbers (as a data type) ... work with regular strings. It will take a long time to figure it out, but it will be achievable :)

0
source

All Articles