teacher_code_python/哥德巴赫高效.py

22 lines
648 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def sieve_of_eratosthenes(n):
primes = [True] * (n + 1) # 创建一个布尔列表初始时所有元素设为True
p = 2
while p * p <= n:
# 如果 primes[p] 未被标记为 False, 则是一个质数
if primes[p]:
# 更新所有 p 的倍数,从 p*p 开始标记为 False
for i in range(p * p, n + 1, p):
primes[i] = False
p += 1
# 收集所有质数
prime_numbers = [p for p in range(2, n + 1) if primes[p]]
return prime_numbers
n=int(input())
numbers=sieve_of_eratosthenes(n)
for i in numbers:
if n-i in numbers:
print(i,n-i)
break