题目:一个数如果恰好等于它的因子之和,这个数就称为“完数” 。例如 6=1+2+3。编程找出 1000 以内的所有完数

python 代码块:

from functools import reduce
import math 

def mutiply(a,b): #定义匿名乘法函数
    return a*b

for i in range(2,1001):  #range(start,stop)一般用于for循环 不包括stop
    list = [1]
    for j in range(2,math.ceil(i/2)+1):
        if i%j==0:
            list.append(j)
            if i==reduce(mutiply,list):
                print(i)
                print(list)

猜你喜欢

转载自blog.csdn.net/qq_35182128/article/details/86693198