Search for three-digit daffodils in php

Source: Internet
Author: User
Tags php example
Search for three-digit daffodils in php

In C language, when it comes to algorithms, one of the problems is to calculate the three-digit "Daffodils". what is "Daffodils? The number of daffodils is the number of n (> = 3) digits, which is equal to the sum of the n power of each number. for example, 153 is a daffodils, 153 = 1*1*1 + 5*5*5 + 3*3*3;

Example 1: use the C language to find the three-digit fairy flower.

  1. # Include
  2. Main ()
  3. /*
  4. * You can use three for loops to evaluate the three-digit number;
  5. * 153 is the number of daffodils. 153 = 1*1*1 + 5*5*5 + 3*3*3;
  6. */
  7. {
  8. Int a, B, c;
  9. For (a = 0; a <= 9; a ++)
  10. {
  11. For (B = 0; B <= 9; B ++)
  12. {
  13. For (c = 0; c <= 9; c ++)
  14. {
  15. // The following judgment is the main algorithm for implementation
  16. If (a * a + B * B + c * c = 100 * a + 10 * B + c)
  17. {// Bbs.it-home.org
  18. Printf ("result: % d", 100 * a + 10 * B + c );
  19. }
  20. }
  21. }
  22. }
  23. }

Example 2: a php example of daffodils.

  1. $ A = array ();
  2. For ($ I = 0; $ I <= 9; $ I ++)
  3. {
  4. For ($ j = 0; $ j <= 9; $ j ++)
  5. {
  6. For ($ m = 0; $ m <= 9; $ m ++)
  7. {
  8. If ($ I * $ I + $ j * $ j + $ m * $ m = 100 * $ I + 10 * $ j + $ m)
  9. {
  10. $ A [] = 100 * $ I + 10 * $ j + $ m;
  11. }
  12. }
  13. }
  14. }
  15. Print_r ($ );

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.