Latest web development tutorials

Python includes exercises 19

Python 100 Li Python 100 Li

Title: If a number is exactly equal to the sum of its factors, this number is called "complete count." For example, 6 = 1 + 2 + 3. Programming to find all finished within a few 1000.

Program analysis: Refer to program Python includes exercises 14 .

Source Code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from sys import stdout
for j in range(2,1001):
    k = []
    n = -1
    s = j
    for i in range(1,j):
            if j % i == 0:
                n += 1
                s -= i
                k.append(i)
    
    if s == 0:
        print j
        for i in range(n):
            stdout.write(str(k[i]))
            stdout.write(' ')
        print k[n]

The above example output is:

6
1 2 3
28
1 2 4 7 14
496
1 2 4 8 16 31 62 124 248

Python 100 Li Python 100 Li