python编译1000以内的完数,输出6
时间: 2023-06-07 22:08:54 浏览: 82
编程找出1000之内的所有完数,并按下面格式输出其因子:6 its factors are 1,2,3
5星 · 资源好评率100%
以下是 Python 代码实现:
```python
def is_perfect_number(n):
divisors = [1]
for i in range(2, int(n/2)+1):
if n % i == 0:
divisors.append(i)
return sum(divisors) == n
for i in range(2, 1001):
if is_perfect_number(i):
print(i)
```
输出为:
```
6
28
496
```
阅读全文