一个数如果恰好等于它的因子之和,这个数就称为完数。例如,6是完数,因为6=1+2+3。编程求1000之内的所有完数,并按下面格式输出其因子:6 its factors are 1,2,3
时间: 2023-04-22 15:05:10 浏览: 77
编程找出1000之内的所有完数,并按下面格式输出其因子:6 its factors are 1,2,3
5星 · 资源好评率100%
以下是Python代码实现:
```python
for i in range(2, 1001):
factors = []
for j in range(1, i):
if i % j == :
factors.append(j)
if sum(factors) == i:
print(i, "its factors are", ",".join(str(x) for x in factors))
```
输出结果为:
```
6 its factors are 1,2,3
28 its factors are 1,2,4,7,14
496 its factors are 1,2,4,8,16,31,62,124,248
```
阅读全文