Home  >  Article  >  Backend Development  >  php实现水仙花数示例分享_PHP

php实现水仙花数示例分享_PHP

WBOY
WBOYOriginal
2016-06-01 11:54:311078browse

自幂数,又称阿姆斯特朗数,民间通称水仙花数。实则只有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