Python练习_1 水仙花数_2

之前的程序只能显示指定位数的自幂数,现在想要找到所有的自幂数。

先改为显示所有自幂数。

程序如下:(前面之所以time,sys是因为下一步要添加进度条。等待时间的确很长。。。)

import time, datetime, sys

figure = int(input('Input digit(>2): '))
start_time = datetime.datetime.now()
max_val = 10 ** (figure + 1)
min_val = 100
gap = max_val - min_val
count = 0

while figure > 2:
    for n in range(2, figure + 1):
        for val in range(min_val, 10 ** n):
            num_list = list(map(int, str(val)))
            sum = 0
            for digit in num_list:
                sum += digit ** n
            if sum == val:
                count += 1
                print('[', n, ']', 'PPDI', count, ' = ', val)
    break
end_time = datetime.datetime.now()
run_time = end_time-start_time
print('Done!')
print('Run Time : ', run_time)
结果 如下:

Input digit(>2): 8
[ 3 ] PPDI 1  =  153
[ 3 ] PPDI 2  =  370
[ 3 ] PPDI 3  =  371
[ 3 ] PPDI 4  =  407
[ 4 ] PPDI 5  =  1634
[ 4 ] PPDI 6  =  8208
[ 4 ] PPDI 7  =  9474
[ 5 ] PPDI 8  =  4150
[ 5 ] PPDI 9  =  4151
[ 5 ] PPDI 10  =  54748
[ 5 ] PPDI 11  =  92727
[ 5 ] PPDI 12  =  93084
[ 6 ] PPDI 13  =  548834
[ 7 ] PPDI 14  =  1741725
[ 7 ] PPDI 15  =  4210818
[ 7 ] PPDI 16  =  9800817
[ 7 ] PPDI 17  =  9926315
[ 8 ] PPDI 18  =  24678050
[ 8 ] PPDI 19  =  24678051
[ 8 ] PPDI 20  =  88593477
Done!
Run Time :  0:12:04.947313

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值