20s内找出14个完数,计算完数。所谓完数就是该数恰好等于除自身外的因子之和。例如:6=1+2+3,其中1、2、3为6的因子。输入一个整数n,输出最小的n(1 <= n <=14)个完数的表达式(参考用例格式)
时间: 2023-12-11 12:30:55 浏览: 114
完数的计算
```python
# 寻找完数
def perfect_number(n):
res = []
i = 1
while len(res) < n:
s = 0
for j in range(1, i):
if i % j == 0:
s += j
if s == i:
res.append(i)
i += 1
return res
# 输出完数表达式
def expression(num):
res = []
for n in num:
s = str(n) + '='
for i in range(1, n):
if n % i == 0:
s += str(i) + '+'
res.append(s[:-1])
return res
# 输出14个完数的表达式
perfect_num = perfect_number(14)
perfect_expression = expression(perfect_num)
for exp in perfect_expression:
print(exp)
```
阅读全文