downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

GMP Functions> <Predefined Constants
[edit] Last updated: Fri, 26 Apr 2013

view this page in

Examples

Example #1 Factorial function using GMP

<?php
function fact($x
{
    
$return 1;
    for (
$i=2$i $x$i++) {
        
$return gmp_mul($return$i);
    }
    return 
$return;
}

echo 
gmp_strval(fact(1000)) . "\n";
?>

This will calculate factorial of 1000 (pretty big number) very fast.



add a note add a note User Contributed Notes Examples - [1 notes]
up
1
rks at rks dot org
4 years ago
I believe that "fact" computes the factorial of $x-1 not of $x.  The for statement should be

for ($i=2; $i <= $x; $i++)

not

for ($i=2; $i < $x; $i++)

If you try to compute the factorial of 2 for example, the for loop will not be executed and the result will be 1.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites