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

search for in the

Fonctions GMP> <Constantes pré-définies
Last updated: Fri, 14 Aug 2009

view this page in

Exemples

Exemple #1 Factorielle avec 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";
?>

Cet exemple va calculer factorielle de 1000 (un grand nombre) très vite.



add a note add a note User Contributed Notes
Exemples
rks at rks dot org
13-Dec-2008 06:25
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.

Fonctions GMP> <Constantes pré-définies
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites