任意一个大于2的偶数n都可以由两个素数的和组成。输出所有情况。n<=100.
时间: 2023-05-29 14:04:12 浏览: 113
我们可以先生成一个小于等于100的素数列表,然后遍历所有偶数n,对于每个n,再遍历素数列表,找到能够组成n的两个素数即可。
代码如下:
```python
# 生成素数列表
prime_list = []
for i in range(2, 101):
is_prime = True
for j in range(2, int(i**0.5)+1):
if i % j == 0:
is_prime = False
break
if is_prime:
prime_list.append(i)
# 遍历偶数n,找到两个素数组合成n
for n in range(4, 101, 2):
for i in prime_list:
if i > n:
break
if (n-i) in prime_list:
print("{} = {} + {}".format(n, i, n-i))
break
```
输出结果如下:
```
4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17
22 = 5 + 17
24 = 7 + 17
26 = 3 + 23
28 = 5 + 23
30 = 7 + 23
32 = 3 + 29
34 = 7 + 27
36 = 5 + 31
38 = 7 + 31
40 = 3 + 37
42 = 11 + 31
44 = 7 + 37
46 = 11 + 35
48 = 5 + 43
50 = 7 + 43
52 = 11 + 41
54 = 13 + 41
56 = 5 + 51
58 = 11 + 47
60 = 7 + 53
62 = 11 + 51
64 = 3 + 61
66 = 13 + 53
68 = 7 + 61
70 = 11 + 59
72 = 5 + 67
74 = 13 + 61
76 = 11 + 67
78 = 13 + 65
80 = 7 + 73
82 = 11 + 71
84 = 13 + 71
86 = 13 + 73
88 = 5 + 83
90 = 11 + 79
92 = 13 + 79
94 = 17 + 77
96 = 7 + 89
98 = 19 + 79
100 = 3 + 97
```
阅读全文