如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数
nums = []
for i in range(100,999):
nums = list(str(i))
if int(nums[0])**3 + int(nums[1])**3 + int(nums[2])**3 == i:
print(i)
D:\Python\Python36\python.exe D:/Python_test/day_lesson/test.py
153
370
371
407
Process finished with exit code 0
for i in range(100, 1000):
sum = 0
temp = i
while temp:
sum = sum + (temp%10) ** 3
temp //= 10
if sum == i:
print(i)