No_21:Amicable numbers

题目描述:

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000

我的第一次尝试:

dlist=[ [] for x in xrange(0,10001)]
dlist[0]=[0]
dlist[1]=[1]
dlist[2]=[1,2]
def countd(num):
    if dlist[num]!=[]:
        return dlist[num]
    else:
        tempint=2
        maxint=int(math.sqrt(num)+1)
        while 1:
            if tempint==maxint:
                return [num]
            else:
                if not num%tempint:

                    templist= [tempint]
                    templist.extend(countd(num/tempint))
                    return templist
                else:
                    tempint+=1
                    #print num
def issushu(num):
    maxint=int(math.sqrt(num)+1)
    for tempint in xrange(2,maxint+1):
        if not num%tempint:
            return False
    return True
matchedlist=[]
for num in xrange(3,10001):
    if dlist[num]==[]:
        dlist[num]=countd(num)
        comparecount=reduce(lambda  x,y:x+y,dlist[num])
        if dlist[comparecount]==[]:
            dlist[comparecount]=countd(comparecount)
        comparecount=reduce(lambda x,y:x+y,countd(comparecount))

        if comparecount==num:
            matchedlist.append((num,comparecount))
matchedlist=filter(lambda x:not issushu(x[0]),matchedlist)
print len(matchedlist)
。。结果连思路都错了。。
这是我的第二次尝试:
import math

maxint=10000
dlist=[ [0] for x in xrange(0,maxint+1)]
for tempint in xrange(1,(maxint+1)/2):
    for secondtemp in xrange(tempint*2,maxint+1,tempint):
        dlist[secondtemp].append(tempint)
matchedlist=[]
for tempint in xrange(3,maxint+1):
    comparenum=reduce(lambda x,y:x+y,dlist[tempint])
    if comparenum<=maxint:
        if reduce(lambda x,y:x+y,dlist[comparenum])==tempint and tempint!=comparenum:            
            matchedlist.append((tempint,comparenum))

print len(matchedlist)
输出结果为:10

但是还是错了。
调试了一下,发现对于(a,b)我会添加两次,一次为(a,b),一次为(b,a)
改一下。去掉重复试下:

import math

maxint=10000
dlist=[ [0] for x in xrange(0,maxint+1)]
for tempint in xrange(1,(maxint+1)/2):
    for secondtemp in xrange(tempint*2,maxint+1,tempint):
        dlist[secondtemp].append(tempint)
matchedlist=[]
for tempint in xrange(3,maxint+1):
    comparenum=reduce(lambda x,y:x+y,dlist[tempint])
    if comparenum<=maxint:
        if reduce(lambda x,y:x+y,dlist[comparenum])==tempint and tempint!=comparenum:
            matchedlist.append((min(tempint,comparenum),max(tempint,comparenum)))
matchedset=set(matchedlist)
print len(matchedset)
结果为:

5
发现我题目好像理解错了。
要求的是数的和啊。
再改一下,求数的合:

import math

maxint=10000
dlist=[ [0] for x in xrange(0,maxint+1)]
for tempint in xrange(1,(maxint+1)/2):
    for secondtemp in xrange(tempint*2,maxint+1,tempint):
        dlist[secondtemp].append(tempint)
matchedlist=[]
for tempint in xrange(3,maxint+1):
    comparenum=reduce(lambda x,y:x+y,dlist[tempint])
    if comparenum<=maxint:
        if reduce(lambda x,y:x+y,dlist[comparenum])==tempint and tempint!=comparenum:
            matchedlist.append((min(tempint,comparenum),max(tempint,comparenum)))
matchedset=set(matchedlist)
x= reduce(lambda x,y:x+y,[ x[0]+x[1] for x in matchedset])
print x
结果为:
31626



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值