php 实现欧拉函数Euler

前端之家收集整理的这篇文章主要介绍了php 实现欧拉函数Euler前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

欧拉函数ph(n)的意思是所有小于n且与n互质的个数。
比如说ph(10) = 4{1,3,7,9与10互质}


代码如下:

 

 1 function Euler($x)
 2 {
 3     $res = ;
 4     $now = 2 5     while ($x > 1) {
 6         if ($x % $now == 0 7             $res /= $now 8             $res *= ($now - 1);
 9             10                 $x /= 11             }
12         }
13         $now++14     }
15     return $res16 }
17 
18 $res = Euler(1019 
20 var_dump($res);

 

 

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);

 

done!

猜你在找的PHP相关文章