求1到1000的完美数,用Python生成代码
时间: 2024-05-14 22:13:09 浏览: 41
1小时学会Python
下面是Python代码:
```python
def is_perfect(num):
factors = []
for i in range(1, num):
if num % i == 0:
factors.append(i)
return sum(factors) == num
perfect_numbers = []
for i in range(1, 1000):
if is_perfect(i):
perfect_numbers.append(i)
print(perfect_numbers)
```
阅读全文