一个数如果恰好等于它的因子和,这个数就称为“完数”。例如,6的因子为1,2,3,而6=1+2+3,所以6是“完数”。 编程输出1000以内的所有完数,每个完数输出一行,并按下面的格式输出其因子: 6 its factors are 1,2,3
时间: 2023-04-16 09:04:57 浏览: 60
编程找出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 == 0:
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
```
阅读全文