NO_30Digit fifth powers

题目:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1 4 + 6 4 + 3 4 + 4 4
8208 = 8 4 + 2 4 + 0 4 + 8 4
9474 = 9 4 + 4 4 + 7 4 + 4 4

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

代码为:
import datetime

def ismatched(num):
    intlist=[int(i) for i in str(num)]
    if reduce(lambda x,y:x+y,[n**5 for n in intlist])==num:
        return True
    else:
        return False
start_time=datetime.datetime.now()
endnum=2
while 1:
    if 10**(endnum)>reduce(lambda x,y:x+y,[ 9**5 for x in xrange(endnum) ]):
        break
    else:
        endnum+=1
relist=[ x for x in xrange(10,10**(endnum)) if ismatched(x)]
print reduce(lambda  x,y:x+y,relist)
end_time=datetime.datetime.now()
print end_time-start_time
结果:
443839
0:00:05.580000

为什么这种代码居然比我还快呢?

import math,datetime



start_time=datetime.datetime.now()
def prob30(x):
        s = 0
        for i in range(2,1000000):
                t = 0
                for a in str(i):
                        t += int(a)**x
                if t == i:
                        s += t
        print s
prob30(5)
end_time=datetime.datetime.now()
#print abundantlist
print end_time-start_time
这个结果居然比我的快?:

443839
0:00:03.806000
牛人代码2:

import math,datetime



start_time=datetime.datetime.now()
c=0
for q in range (0,10):
  for w in range (0,q+1):
    for e in range (0,w+1):
      for r in range (0,e+1):
         for t in range (0,r+1):
            for y in range (0,t+1):
                v= (q**5+w**5+e**5+r**5+t**5+y**5)
                b=0
                n=str(v)
                m=0
                while m < len(n) and b <= v:
                       b+=int(n[m])**5
                       m+=1
                if b == v :
                      c += v
print c-1
end_time=datetime.datetime.now()
print end_time-start_time
结果为:

443839
0:00:00.030000
超NB



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值