Home  >  Article  >  Backend Development  >  PHP implementation of narcissus number example sharing_PHP tutorial

PHP implementation of narcissus number example sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:42968browse

Auto-power numbers, also known as Armstrong numbers, are commonly known as narcissus numbers among the people. In fact, only 3-digit exponentiates are narcissus numbers. 4 digits, 5 digits, 6 digits, etc. have different names.

Copy code The code is as follows:

//Armstrong number: a k-digit number, it The sum of the k-th powers of the numbers in each digit is equal to itself. (Example: 1^3 + 5^3 + 3^3 = 153)
class Armstrong {

static function index(){
for ( $i = 100; $i < 100000; $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();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/749189.htmlTechArticleAuto-power number, also known as Armstrong number, commonly known as narcissus number among the people. In fact, only 3-digit exponentiates are narcissus numbers. 4 digits, 5 digits, 6 digits, etc. have different names. Copy the code The code is as follows: ?...
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