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();
http://www.bkjia.com/PHPjc/749189.htmlwww.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: ?...