這篇文章主要介紹了PHP簡單實現歐拉函數Euler功能,簡單說明了歐拉函數的概念、原理,并結合實例形式分析了php實現歐拉函數的相關操作技巧,需要的朋友可以參考下
本文實例講述了PHP簡單實現歐拉函數Euler功能,分享給大家供大家參考,具體如下:
歐拉函數ph(n)的意思是所有小于n且與n互質的個數。
比如說ph(10) = 4{1,3,7,9與10互質}
代碼如下:
- <?php
- function 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)
新聞熱點
疑難解答