[python practice questions] program 19

#Question : A number is called a "complete number" if it is exactly equal to the sum of its factors. For example, 6=1+2+3. Program to find all the numbers within 1000. 
def suoyou(n):#find all factors
    s = []
    for i in range(1,n+1):
        if n % i ==0:
            s.append(i)
    s = s [: len (s) -1 ]
    x = 0
     # print('All silver',s) 
    for i in s:
        x +=i
    return x

def zhengchu(n):#factoring

    for i in range(2, n + 1):
        if n % i == 0:
            k = int(n / i)
            m.append(i)
            return zhengchu(k)
        else:
            continue
    if m == []:
        n = n + 1
    else:
        y = [str(x) for x in m]
        # print (y)
        t1 = '*'.join(y)
        n1 = eval (t1)
         # print('suoyou',suoyou(int(X))) 
        if int(n1) == suoyou(int(X)):
             print ( ' %s is a complete number ' % X)

for n in range(1,1001):
    # print('ha',n)

    X = str(n)
    m = []

    zhengchu(n)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325033212&siteId=291194637