4 examples of how many daffodils are implemented in PHP

Source: Internet
Author: User

What is the number of daffodils?

Narcissus number refers to an n-digit number (n≥3), and the sum of the number of its digits on each bit is equal to its own. (ex: 1^3 + 3^3+ 5^3 = 153), this article collects 4 examples of how many daffodils are implemented in PHP.

Example 1,php to achieve 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 to achieve 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); Break up the Hundred
    8. $tens = Floor ($n/10)% 10; Break out 10-bit
    9. $ones = Floor ($n% 10); Split the bits.
    10. Return (BOOL) (Cube ($hundreds) +cube ($tens) +cube ($ones) = = $n);
    11. }
    12. for ($i = +, $i <, + + $i)
    13. {
    14. if (is_narcissistic ($i))
    15. echo $i. " \ n ";
    16. }
    17. ?>
Copy Code

Example 3,php to achieve the number of daffodils:

    1. Armstrong number: A K-digit, and the sum of the number of its digits on each bit is equal to its own. (Example: 1^3 + 5^3 + 3^3 = 153)
    2. Class Armstrong {
    3. static function index () {
    4. for ($i = n, $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 to achieve the number of daffodils:

    1. PHP implements Narcissus number _bbs.it-home.org
    2. function Winter ($num)
    3. {
    4. if ($num <1000) {
    5. Define Bits
    6. $ge = $num% 10;
    7. Define 10-bit
    8. $ten = (($num%100)-$ge)/10;
    9. Define the Hundred
    10. /*floor rounding, ignoring all numbers after the decimal point */
    11. $hundred =floor ($num/100);
    12. $sum 1= $ge * $ge * $ge + $ten * $ten * $ten + $hundred * $hundred * $hundred;
    13. if ($sum 1== $num) {
    14. return 1;
    15. } else{
    16. return 0;
    17. }
    18. } else{
    19. return-1;
    20. }
    21. }
    22. if (Winter (371) ==-1) {
    23. echo "number greater than 1000";
    24. }else{
    25. if (Winter (371)) {
    26. echo "Yes";
    27. }
    28. else{
    29. echo "No";
    30. }
    31. }
    32. ?>
Copy Code
  • 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.