若某自然数除它本身之外的所有因子之和等于该数,则称该数为完数。输出1000以内的完数python
时间: 2024-03-18 09:19:27 浏览: 91
计算机考试
代码如下:
```python
def perfect_numbers(limit):
result = []
for num in range(1, limit):
divisors_sum = sum([i for i in range(1, num) if num % i == 0])
if num == divisors_sum:
result.append(num)
return result
print(perfect_numbers(1000))
```
运行结果为:
```
[6, 28, 496]
```
阅读全文