Question: Print out all the "Daffodil Numbers". The so-called "Daffodil Numbers" refers to a three-digit number whose cubic sum of the digits is equal to the number itself. For example: 153 is a "daffodil number", because 153=1 cube + 5 cube + 3 cube.
Program analysis: Use the for loop to control 100-999 numbers, and each number is decomposed into units, tens, and hundreds.
Program source code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for n in range(100, 1000):
i = n / 100
d = n / 10% 10
k = n % 10
if n == i ** 3 + j ** 3 + k ** 3:
print n
The output of the above example is:
153
370
371
407