#T56. 哥德巴赫猜想
哥德巴赫猜想
Description
One of the propositions of Goldbach's Conjecture is: every even number greater than 6 can be expressed as the sum of two prime numbers. Write a program to represent all even numbers from 6 to 100 as the sum of two prime numbers.
Input Format
(None)
Output Format
Output line by line:
For example:
6=3+3
8=3+5
...
(Each number should be split only once, ensuring the first addend is the smallest possible)
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
for even in range(6, 101, 2):
for first in range(2, even // 2 + 1):
second = even - first
if is_prime(first) and is_prime(second):
print(f"{even}={first}+{second}")
break
The code above will generate the required output by:
- Defining a helper function
is_prime()to check if a number is prime - Iterating through even numbers from 6 to 100
- For each even number, finding the smallest prime
firstsuch thateven - firstis also prime - Printing the result in the specified format and breaking to ensure only one split per number
(无)
(无)