难点:
- 限制1s的周期,所以要在约数的部分进行简化,用1到
sqrt(num)
,进行枚举,不要选择1到num; - 这个题目要求的是:在某个数之内的完美数的个数,容易混淆题意。
def gongyueshu(num):
res = []
for i in range(1, int(num**0.5)+1):
if num%i == 0:
res.append(i)
if i != 1:
res.append(int(num/i))
return res
while True:
try:
count = 0
num = int(input())
for n in range(2, num+1):
res = gongyueshu(n)
if (res != []) and (sum(res) == n):
count += 1
else:
continue
print(count)
except:
break