Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Goldbach Conjecture (Even as Sum of Two Primes)

Search for a two-prime sum of an even number with Python. Students scan only one pair direction and distinguish checking an example from proving Goldbach.

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

In this one

  • Search complementary values
  • Stop at a first solution
  • Separate a check from proof

How it works

  1. 1

    Try i from two through half the input.

  2. 2

    Test i and num - i for primality.

  3. 3

    Print the first successful equation and break.

Edge cases

Does the code print every valid pair?

Does the code print every valid pair?

No. It stops at the first one.

Does this prove Goldbach's conjecture?

Does this prove Goldbach's conjecture?

No. It checks only the entered case.

What's going on

The loop tries i from two through half the entered number. It checks whether both i and num - i are prime.

On the first match, the equation prints and break stops further searching. This finds one representation, not every representation or a proof of the conjecture. The prompt asks for even input above two, but the code does not validate that restriction before searching.

Does the code print every valid pair? No. It stops at the first one.

Does this prove Goldbach's conjecture? No. It checks only the entered case.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why stop candidates near half the number?

Pick an answer to check it.

Question 2

Does the code print every valid pair?

Pick an answer to check it.

Questions people ask

Does the code print every valid pair?

No. It stops at the first one.

Does this prove Goldbach's conjecture?

No. It checks only the entered case.

Why stop candidates near half the number?

Larger choices would reverse already considered pairs.