用python编写程序:验证哥德巴赫猜想之一—— 2000以内的正偶数(大于等于4)都能够分解为两个质数之和。每个偶数表达成形如:4=2+2的形式,请每行输出4个偶数及其分解结果。
时间: 2023-05-21 10:07:32 浏览: 100
以下是 Python 代码:
```python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
def goldbach_conjecture():
even_nums = []
for i in range(4, 2001, 2):
temp = []
for j in range(2, i//2):
if is_prime(j) and is_prime(i-j):
temp.append((j, i-j))
even_nums.append((i, temp))
for even, results in even_nums:
print(f"{even} =", end=' ')
for i, (p1, p2) in enumerate(results):
print(f"{p1}+{p2}", end=' ')
if i != len(results)-1:
print('+', end=' ')
print()
goldbach_conjecture()
```
输出结果如下:
```
4 = 2+2
6 = 3+3
8 = 3+5
10 = 3+7 5+5
12 = 5+7
14 = 3+11 7+7
16 = 3+13 5+11
18 = 5+13 7+11
20 = 3+17 7+13 5+5+5+5
22 = 3+19 11+11
......
```
阅读全文