初始化結果變量為1。
從1循環到N,每次將結果與當前數字相乘,並對結果取模M。
最終的結果即為大數的階乘模M。
<?php
// 定义大数N和模数M
$N = "1000" ;
$M = "100000007" ;
// 使用GMP库初始化结果变量为1
$result = gmp_init(1);
// 循环计算乘法并取模
for ( $i = 1; $i <= $N ; $i ++) {
// 将结果与当前数字相乘
$result = gmp_mul( $result , gmp_init( $i ));
// 取结果的模M
$result = gmp_mod( $result , gmp_init( $M ));
}
// 打印计算结果
echo gmp_strval( $result );
?>