The Goldbach Conjecture (Python Challenge)

The Goldbach Conjecture is an unproven mathematical rule, proposed by Christian Goldbach in 1742, stating that every even integer greater than 2 is the sum of two prime numbers. Despite verification by computers up to, no formal proof exists, making it one of the oldest unsolved problems in number theory!

Every even number greater than 2 can be expressed as the sum of two prime numbers.

Let’s look at some examples:

  • 10 = 3 + 7
  • 28 = 5 + 23
  • 50 = 3 + 47

Our challenge is to write a Python program that tests this conjecture for any even number provided by the end-user of this program. Our program will need to:

    Ask the user to enter an even number greater than 2
    Find two prime numbers that add up to this number
    Display the result

Example Output

Enter an even number: 28
28 = 5 + 23

Prime Numbers?

A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1.

The first 20 prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, and 71.

To help test the Goldbach Conjecture, we will use a function isPrime() that takes a whole positive number as a parameter and returns whether or not this number is a prime number. Here is the code for this function:

def isPrime(n):
   if n < 2:
      return False
   for i in range(2, int(n**0.5) + 1):
      if n % i == 0:
         return False
   return True

You can test this function using the following code:

number = int(input("Enter a whole number"))
if isPrime(number):
   print("This number is a prime number!")
else:
   print("This number is a not a prime number!")

Testing the Goldbach Conjecture

We are now going to implement an algorithm to test the Goldbach Conjecture for any given number. We will base our Python code on the following flowchart:

Python Code

Use the following IDE to complete the code for testing the Goldbach conjecture based on the above flowchart:

Extension Task

Tweak your code to use an iterative approach to check all even numbers from 4 up to 1000 and display their prime pairs.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!