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.
- # Include
- Main ()
- /*
- * You can use three for loops to evaluate the three-digit number;
- * 153 is the number of daffodils. 153 = 1*1*1 + 5*5*5 + 3*3*3;
- */
- {
- Int a, B, c;
- For (a = 0; a <= 9; a ++)
- {
- For (B = 0; B <= 9; B ++)
- {
- For (c = 0; c <= 9; c ++)
- {
- // The following judgment is the main algorithm for implementation
- If (a * a + B * B + c * c = 100 * a + 10 * B + c)
- {// Bbs.it-home.org
- Printf ("result: % d", 100 * a + 10 * B + c );
- }
- }
- }
- }
- }
Example 2: a php example of daffodils.
- $ A = array ();
- For ($ I = 0; $ I <= 9; $ I ++)
- {
- For ($ j = 0; $ j <= 9; $ j ++)
- {
- For ($ m = 0; $ m <= 9; $ m ++)
- {
- If ($ I * $ I + $ j * $ j + $ m * $ m = 100 * $ I + 10 * $ j + $ m)
- {
- $ A [] = 100 * $ I + 10 * $ j + $ m;
- }
- }
- }
- }
- Print_r ($ );
|