php 实现欧拉函数Euler

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z_qifa/article/details/74202924
欧拉函数ph(n)的意思是所有小于n且与n互质的个数。
比如说ph(10) = 4{1,3,7,9与10互质}


代码如下:

 1 function Euler($x)
 2 {
 3     $res = $x;
 4     $now = 2;
 5     while ($x > 1) {
 6         if ($x % $now == 0) {
 7             $res /= $now;
 8             $res *= ($now - 1);
 9             while ($x % $now == 0) {
10                 $x /= $now;
11             }
12         }
13         $now++;
14     }
15     return $res;
16 }
17 
18 $res = Euler(10);
19 
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!

猜你喜欢

转载自blog.csdn.net/z_qifa/article/details/74202924