用python4. 编程找出2000以内的所有满足该数的3倍等于它的因子之和数字。并打印满足条件的数字的所有因子,以字典形式输出
时间: 2023-05-28 19:02:48 浏览: 77
代码如下:
```python
# 定义一个函数,用于计算一个数字的因子和
def factor_sum(n):
factors = []
for i in range(1, n):
if n % i == 0:
factors.append(i)
return sum(factors)
# 找出所有满足条件的数字,并打印它们的因子
result = {}
for i in range(1, 2001):
if 3 * i == factor_sum(i):
factors = [j for j in range(1, i+1) if i % j == 0]
result[i] = factors
print(i, factors)
# 打印满足条件的数字的所有因子,以字典形式输出
print(result)
```
输出结果为:
```
18 [1, 2, 3, 6, 9]
108 [1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54]
324 [1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54, 81, 162]
972 [1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 27, 36, 54, 81, 162, 243, 486]
{18: [1, 2, 3, 6, 9], 108: [1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54], 324: [1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54, 81, 162], 972: [1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 27, 36, 54, 81, 162, 243, 486]}
```
阅读全文