阿姆斯特朗数

如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。 例如1^3 + 5^3 + 3^3 = 153。
1000以内的阿姆斯特朗数: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407。

for i in range(1, 1001):
    i_str = str(i)
    if len(i_str) == 1:
        if i == i:
            print(i)
    elif len(i_str) == 2:
        if int(i_str[0]) ** 2 + int(i_str[1]) ** 2 == i:
            print(i)
    else:
        if int(i_str[0]) ** 3 + int(i_str[1]) ** 3 + int(i_str[2]) ** 3 == i:
            print(i)

猜你喜欢

转载自blog.csdn.net/zhu6201976/article/details/88369653