加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 实现欧拉函数Euler

发布时间:2020-12-13 21:22:25 所属栏目:PHP教程 来源:网络整理
导读:欧拉函数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 }

欧拉函数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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读