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

search for in the

GMP 関数> <定義済み定数
[edit] Last updated: Fri, 10 Feb 2012

view this page in

例1 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";
?>

この例は、1000 の階乗(非常に大きな数です)を非常に高速に計算します。



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

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