哥德巴赫猜想:任一大于2的偶数都可写成两个质数*(质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。)*之和.
- 首先验证是否为素数.(是素数返回True; 否则返回False)
def prime_number(N):
count = 0
for i in range(2, N):
if N % i == 0:
count += 1
if count == 0:
return True
else:
return False
- 当作练手, 传入你想知道的0到N的范围内有几个素数, 且都是哪些素数.
def All_prime_number(N):
count = 0
for i in range(2, N + 1):
if prime_number(i) == True:
count += 1
print(i, end="\t")
print()
print(f"一共有{count}个素数")
- 哥德巴赫猜想
def Goldbach_conjecture(N):
if N % 2 == 0 and N > 2:
for i in range(2, N + 1):
for j in range(i, N + 1):
if prime_number(i) == True and prime_number(j) == True and i + j == N:
print(f"{i} + {j} = {N}")
else:
print("请输入大于2的偶数!")
- 方便读者获取代码
def prime_number(N):
count = 0
for i in range(2, N):
if N % i == 0:
count += 1
if count == 0:
return True
else:
return False
def All_prime_number(N):
count = 0
for i in range(2, N + 1):
if prime_number(i) == True:
count += 1
print(i, end="\t")
print()
print(f"一共有{count}个素数")
def Goldbach_conjecture(N):
if N % 2 == 0 and N > 2:
for i in range(2, N + 1):
for j in range(i, N + 1):
if prime_number(i) == True and prime_number(j) == True and i + j == N:
print(f"{i} + {j} = {N}")
else:
print("请输入大于2的偶数!")
N = int(input("请输入大于2的偶数:"))
Goldbach_conjecture(N)
当输入N为100时候