Python Exercise Example19
# Python2.x Python Exercise Instance 19
[ Python 100 Examples](#)
**Question:** A number is called a "perfect number" if it is exactly equal to the sum of its factors. For example, 6=1οΌ2οΌ3. Write a program to find all perfect numbers within 1000.
**Program Analysis:** Please refer to the program (#).
Program source code:
## Instance
#!/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))stdout.write('')print(k)
The output of the above instance is:
61 2 3281 2 4 7
YouTip