解题思路:记录遍历中的每一个因子的和进行判断就行,遍历因子的时候为了减小时间复杂度,可以遍历到当前数的开平方,但是这时需要b.append(a//i)
注意事项:a % i == 0时还得b.append(a//i),不能只b.append(i),另外完数只出现在偶数中
参考代码:
a1=int(input())
for a in range(2,a1+1,2):
b = [1]
for i in range(2, int(a **0.5) + 1):
if a % i == 0:
b.append(i)
b.append(a//i)
if sum(b) == a:
b.sort()
b = [str(i) for i in b]
print(str(a) + " its factors are " + " ".join(b))
0.0分
1 人评分