%%time
# 水仙花数if __name__ =='__main__':print("result is: ")# 整数的取值范围for n inrange(100,1000):
hun = n //100# 百位
ten =(n - hun *100)//10# 十位
ind = n %10# 个位
m = hun*hun*hun + ten*ten*ten + ind*ind*ind # 求和if n == m:# 各位上的立方和是否与原数n相等print("%d \t"%n, end="\n")
result is:
153
370
371
407
CPU times: user 1.14 ms, sys: 37 µs, total: 1.18 ms
Wall time: 1.17 ms
%%time
# 水仙花数if __name__ =='__main__':print("result is: ")for n inrange(100,1000):# 整数的取值范围
hun = n //100# 百位
ten =(n - hun *100)//10# 十位
ind = n %10# 个位
m = hun**3+ ten**3+ ind**3if n == m:# 各位上的立方和是否与原数n相等print("%d \t"%n, end="\n")
result is:
153
370
371
407
CPU times: user 1.57 ms, sys: 0 ns, total: 1.57 ms
Wall time: 1.56 ms
%%time
# 巧用字符串找水仙花数if __name__ =='__main__':print("result is: ")for n inrange(100,1000):# 整数的取值范围
str_n=str(n)
hun =int(str_n[0])# 百位
ten =int(str_n[1])# 十位
ind =int(str_n[2])# 个位
m = hun**3+ ten**3+ ind**3if n == m:# 各位上的立方和是否与原数n相等print("%d \t"%n, end="\n")
result is:
153
370
371
407
CPU times: user 2.11 ms, sys: 0 ns, total: 2.11 ms
Wall time: 2.03 ms