Funções da GMP> <Constantes pré-definidas
Last updated: Fri, 02 Jan 2009

Exemplos

Exemplo #1 Função factorial usando 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";
?>

Isto irá calcular o fatorial de 1000 (um número grande) muito rápido.



User Contributed Notes
Exemplos
rks at rks dot org
13-Dec-2008 03: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.

Funções da GMP> <Constantes pré-definidas
Last updated: Fri, 02 Jan 2009