用Python找出1000以内所有水仙花数
水仙花数是指一个$n$位数($n \geq 3$),它的每个位上的数字的$n$次幂之和等于它本身。
例如,$153=1^3+5^3+3^3$。
以下是Python代码:
for i in range(100, 1000):
sum = 0
temp = i
while temp:
sum += (temp % 10) ** 3
temp //= 10
if sum == i:
print(i)
输出结果为:
153
370
371
407
原文地址: https://www.cveoy.top/t/topic/bu6C 著作权归作者所有。请勿转载和采集!