Goldbach Conjecture (Even as Sum of Two Primes)

Express even number as sum of two primes.

AdvancedPhase 6 - Category 3: Loop & Conditional MixExample 5 of 10
goldbach-conjecture-even-as-sum-of-two-primes.py
1# Helper function
2def is_prime(n):
3 if n < 2:
4 return False
5 for i in range(2, int(n ** 0.5) + 1):
6 if n % i == 0:
7 return False
8 return True
9
10# Take even number
11num = int(input("Enter an even number (>2): "))
12
13# Find two primes
14found = False
15for i in range(2, num // 2 + 1):
16 if is_prime(i) and is_prime(num - i):
17 print(f"{num} = {i} + {num - i}")
18 found = True
19 break
20
21if not found:
22 print("No representation found")

Output

Enter an even number (>2): 20
20 = 3 + 17

What's going on

Find two primes that sum to even number.

Key Concepts:

Check if i and num-i are prime
Both must be prime
Goldbach conjecture