本文實例講述了PHP簡單實現歐拉函數Euler功能。分享給大家供大家參考,具體如下:
歐拉函數ph(n)的意思是所有小于n且與n互質的個數。
比如說ph(10) = 4{1,3,7,9與10互質}
代碼如下:
<?phpfunction Euler($x){ $res = $x; $now = 2; while ($x > 1) { if ($x % $now == 0) { $res /= $now; $res *= ($now - 1); while ($x % $now == 0) { $x /= $now; } } $now++; } return $res;}$res = Euler(10);var_dump($res);?>
運行結果:
int(4)
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選