Home > Backend Development > PHP Tutorial > 4 examples of php implementation of narcissus number

4 examples of php implementation of narcissus number

WBOY
Release: 2016-07-25 09:13:01
Original
1823 people have browsed it

What is the Narcissus Number?

Daffodil number refers to an n-digit number (n≥3), the sum of the nth power of the digits in each digit is equal to itself. (For example: 1^3 + 3^3+ 5^3 = 153), this article collects 4 examples of php implementing narcissus number.

Example 1, php implements the number of daffodils:

  1. for($q=1;$q<=9;$q++){
  2. for($w=0;$w<=9;$w++){
  3. for($ e=0;$e<=9;$e++){
  4. if($q*$q*$q + $w*$w*$w + $e*$e*$e ==
  5. 100*$q + 10*$w + $e){
  6. echo "$q $w $e "."

    ";

  7. }
  8. }
  9. }
  10. }
  11. ?>
Copy code

Example 2. PHP implements the number of daffodils:

  1. function cube( $n )

  2. {
  3. return $n * $n * $n;
  4. }

  5. function is_narcissistic ( $n )

  6. {
  7. $hundreds = floor( $n / 100); //Decompose the hundreds place
  8. $tens = floor( $n / 10 ) % 10; //Decompose the tens place
  9. $ones = floor( $ n % 10 ); //Decompose the units digit
  10. return (bool)(cube($hundreds)+cube($tens)+cube($ones) == $n);
  11. }
  12. for ( $i = 100; $i < 1000; ++ $i )
  13. {
  14. if ( is_narcissistic($i) )
  15. echo $i."n";
  16. }
  17. ?>
Copy code

Example 3, php implements the number of daffodils:

  1. //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)
  2. class Armstrong {
  3. static function index(){
  4. for ( $i = 100; $i < 100000; $i++ ) {
  5. echo self: :is_armstrong($i) ? $i . '
    ' : '';
  6. }
  7. }
  8. static function is_armstrong($num){
  9. $s = 0;
  10. $k = strlen($num);
  11. $d = str_split($num);
  12. foreach ($d as $r) {
  13. $s += bcpow($r, $k);
  14. }
  15. return $num == $s;
  16. }
  17. }
  18. Armstrong ::index();
Copy code

Example 4, PHP implements daffodil number:

  1. ;?php
  2. function winter($num)
  3. {
  4. if($num<1000){
  5. //Define the ones digit
  6. $ge=$num%10;
  7. //Define the tens digit
  8. $ten=(($num %100)-$ge) /10;
  9. //Define hundreds place
  10. /*floor rounding, ignore all numbers after the decimal point*/
  11. $hundred=floor($num/100);
  12. $sum1=$ge* $ge*$ge+$ten*$ten*$ten+$hundred*$hundred*$hundred;
  13. if($sum1==$num){
  14. return 1;
  15. } else{
  16. return 0;
  17. }
  18. } else {
  19. return -1;
  20. }
  21. }
  22. if(winter(371)==-1) {
  23. echo "A number greater than 1000";
  24. }else{
  25. if(winter(371)) {
  26. echo "Yes" ;
  27. }
  28. else{
  29. echo "No";
  30. }
  31. }
  32. ?>
  33. Copy code
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template