Find the number of three-digit daffodils in php

WBOY
Release: 2016-07-25 09:12:56
Original
4264 people have browsed it

In C language, when it comes to algorithms, one of the questions is to find the three-digit "narcissus number". So what is the "narcissus number"? The daffodil number is a number with n (>=3) digits, which is equal to the sum of the n powers of each number. For example, 153 is a daffodil number, 153=1*1*1+5*5* 5+3*3*3;

Example 1, find the three-digit fairy number in C language.

  1. #include
  2. main()
  3. /*
  4. * To find three-digit numbers, just use 3 for loops;
  5. * 153 is a narcissus number, 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*a + b* b*b + c*c*c == 100*a + 10*b + c)
  17. { /// bbs.it-home.org
  18. printf("The result is: %d", 100*a + 10* b + c);
  19. }
  20. }
  21. }
  22. }
  23. }
Copy code

Example 2, an example of finding the number of daffodils in PHP.

  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*$i + $j*$j*$j + $m*$ m*$m == 100*$i + 10*$j +$m)
  9. {
  10. $a[] = 100*$i + 10*$j +$m;
  11. }
  12. }
  13. }
  14. }
  15. print_r ($a);
Copy code


Related labels:
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