python完全数计算,分两步,一个函数求约数和,其他的做判断
iNOC产品部--完全数计算
http://www.nowcoder.com/questionTerminal/7299c12e6abb437c87ad3e712383ff84
encoding:utf-8
def com(n): #求约数的函数
sum = 0 for i in range(1,n/2+1): if n%i == 0: # print i sum = sum+i # i = i+1 return sum
if name == 'main':
while True:
try:
s = input()
if s>0 and s<=500000:
i = 1
count = 0
while i<=s:
sum = com(i) # print 'sum,i',sum,i if sum == i: count = count +1 # print 'sum==i' i= i+1 print count else: print '-1' except: break