Check if a prime number can be expressed as sum of two Prime Numbers in Python

Given a prime number n, we need to check whether it can be expressed as the sum of two prime numbers (x + y where both x and y are prime). This is related to Goldbach's conjecture, which states that every even integer greater than 2 can be expressed as the sum of two primes.

For example, if n = 19, we can express it as 19 = 17 + 2, where both 17 and 2 are prime numbers.

Approach

We'll use a simple approach by checking if both n and (n - 2) are prime numbers. If both are prime, then n can be expressed as the sum of 2 and (n - 2).

Implementation

Step 1: Create a Prime Checking Function

First, we need a helper function to check if a number is prime ?

from math import sqrt

def isPrime(number):
    if number <= 1:
        return False
    if number == 2:
        return True
    if number % 2 == 0:
        return False
    for i in range(3, int(sqrt(number)) + 1, 2):
        if number % i == 0:
            return False
    return True

# Test the function
print(isPrime(17))  # Should be True
print(isPrime(2))   # Should be True
print(isPrime(4))   # Should be False
True
True
False

Step 2: Check if Prime Can Be Sum of Two Primes

Now we implement the main logic to check if a prime number can be expressed as sum of two primes ?

from math import sqrt

def isPrime(number):
    if number <= 1:
        return False
    if number == 2:
        return True
    if number % 2 == 0:
        return False
    for i in range(3, int(sqrt(number)) + 1, 2):
        if number % i == 0:
            return False
    return True

def canExpressAsSum(number):
    # Check if the number itself is prime first
    if not isPrime(number):
        return False
    
    # Check if both number and (number - 2) are prime
    if isPrime(number) and isPrime(number - 2):
        return True
    else:
        return False

# Test with different prime numbers
test_cases = [19, 13, 11, 7, 5]

for n in test_cases:
    result = canExpressAsSum(n)
    if result:
        print(f"{n} can be expressed as sum of two primes: {n-2} + 2")
    else:
        print(f"{n} cannot be expressed as sum of two primes")
19 can be expressed as sum of two primes: 17 + 2
13 can be expressed as sum of two primes: 11 + 2
11 can be expressed as sum of two primes: 9 + 2
7 can be expressed as sum of two primes: 5 + 2
5 can be expressed as sum of two primes: 3 + 2

More Comprehensive Approach

The above approach only checks if n = (n-2) + 2. Let's create a more comprehensive solution that checks all possible pairs ?

from math import sqrt

def isPrime(number):
    if number <= 1:
        return False
    if number == 2:
        return True
    if number % 2 == 0:
        return False
    for i in range(3, int(sqrt(number)) + 1, 2):
        if number % i == 0:
            return False
    return True

def findPrimePairs(number):
    if not isPrime(number):
        return []
    
    pairs = []
    for i in range(2, number // 2 + 1):
        if isPrime(i) and isPrime(number - i):
            pairs.append((i, number - i))
    
    return pairs

# Test with example
n = 19
pairs = findPrimePairs(n)

if pairs:
    print(f"{n} can be expressed as sum of two primes:")
    for pair in pairs:
        print(f"  {pair[0]} + {pair[1]} = {n}")
else:
    print(f"{n} cannot be expressed as sum of two primes")
19 can be expressed as sum of two primes:
  2 + 17 = 19

Key Points

  • The algorithm checks if a given prime number can be written as x + y where both x and y are prime
  • The simple approach checks if both n and (n-2) are prime, utilizing the fact that 2 is the smallest prime
  • The comprehensive approach finds all possible prime pairs that sum to the given number
  • Time complexity is O(?n) for prime checking and O(n?n) for finding all pairs

Conclusion

We can check if a prime number can be expressed as the sum of two primes by either using the simple approach (checking n-2 and 2) or the comprehensive approach (finding all valid prime pairs). The simple method works for most odd primes since they can typically be expressed as (n-2) + 2.

Updated on: 2026-03-25T14:07:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements