4 examples of the number of daffodils implemented in PHP share

Source: Internet
Author: User

The

  Narcissus number refers to an n-digit (n≥3), in which the sum of the n powers of the number on each bit is equal to itself. (For example: 1^3 + 3^3+ 5^3 = 153) This article mainly introduces the realization of the number of daffodils in PHP 4 examples to share, the need for friends can refer to the next

Example 1:     Code as follows: <?php for ($q =1 $q <=9; $q + +) {    for ($w =0; $w <=9; $w + +) {      for ($e =0 $e <=9; $e + +) {        if ($q * $q * $q + $w * $w * $w + $e * $e * $e =          100* $q + 10* $w + $e) {            echo "$q $w $e". <p> ";        }       {   }?>     Example 2:   Code as follows: <?php func tion Cube ($n) {    return $n * $n * $n;}   function is_narcissistic ($n) {    $hundreds = Floor ($n/100);    //decomposition of the hundred     $tens = Floor ($n/10)% 10;    //10-bit     $ones = floor ($n% 10);    //decomposition of single-digit     return (BOOL) (Cube ($hundreds) +cube ($tens) +cube ($ones) = = $n); }     for ($i = $i < 1000 + + $i) {    if (is_narcissistic ($i))       &NBS P echo $i. " n "; }?>     Example 3:    The code is as follows: <?php//Armstrong number: A K-digit, the sum of the K-power of the number on each bit of it is equal to itself. (For example: 1^3 + 5^3 + 3^3 = 153) class Armstrong { static function index () {  for ($i = $i < 100000; $i + +) {   echo self::is_armstrong ($i)? $i. ' <br> ': ';  } &nbsp  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; &NBSP}} armstrong::index ();     Example 4:   Code as follows: <html>   <head>   <title></title> </head>   & lt;body>   <?php    function Winter ($num)  {       if ($num <1000) {&nbsp ;      //definition single-digit         $ge = $num% 10;        //definition 10-bit         $ten = (($num%100)-$ge)/10;        //defines the hundred        /*floor rounding, ignoring all numbers after the decimal point/    &NBSP   $hundred =floor ($num/100);         $sum 1= $ge * $ge * $ge + $ten * $ten * $ten + $hundred * $hundred * $hundred;        if ($sum 1== $num) {               return 1;                 else{                    & nbsp   return 0;                                   & nbsp       else{                       return-1;                                 &NBSP nbsp          if (Winter (371) ==-1) {                 echo "A number greater than 1000";            }else{                  if (Winter (371)) {                        &NBSP ; echo "Yes";                          }       els e{   echo "No"   &nbsp}        }  ?>   </body> </html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.