Home >php教程 >php手册 >php实现水仙花数示例分享

php实现水仙花数示例分享

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:23:311806browse

这篇文章主要介绍了php实现水仙花数示例,需要的朋友可以参考下

自幂数,又称阿姆斯特朗数,民间通称水仙花数。实则只有3位自幂数才是水仙花数。4位5位6位等等各有别的叫法。

复制代码 代码如下:


//阿姆斯特朗数:一个k位数,,它的每个位上的数字的k次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
class Armstrong {

 static function index(){
  for ( $i = 100; $i    echo self::is_armstrong($i) ? $i . '
' : '';
  }
 }
 static function is_armstrong($num){
  $s = 0;
  $k = strlen($num);
  $d = str_split($num);
  foreach ($d as $r) {
   $s += bcpow($r, $k);
  }
  return $num == $s;
 }

}
Armstrong::index();

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn