The so-called narcissus number is a number raised to a power, also known as Armstrong number. It is also 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.
Example, PHP code to implement narcissus number.
-
- //Armstrong number: a k-digit number, the sum of the k-th power 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 . '
' : '';
- }
- } // edit by bbs.it-home.org
- 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();
Copy code
|