Can A Number Be Expressed As A Sum Of Two Prime Numbers? | Python Program

Number be expressed as a sum of two prime numbers in python :

 

Here, we will discuss the program to check whether a number be expressed as a sum of two prime number in python. Prime number is a number which only have two divisors i.e. a number which can not be divided by any other number other than 1 or itself is a prime number.

There are many theories which express numbers as a sum of two primes like Goldbach’s Conjecture which states that any even number greater than 2 can be expressed as a sum of two primes.

Here we will check for all the numbers if they can be expressed as sum of two primes or not.

Number be expressed as a sum of two prime numbers in python

Algorithm

  • Step 1:- Start.
  • Step 2:- Take user input.
  • Step 3:- Find all the prime numbers less than given number.
  • Step 4:- Try all possible combinations to add this prime number.
  • Step 5:- Check if addition is equal to given number.
  • Step 6:- If TRUE print Those prime numbers.
  • Step 7:- If FALSE print not possible.
  • Step 8:- End.
number be expressed as a sum of two prime numbers

Program to check if the given number is sum of two prime number in python :

#take input
Number = int(input('Enter the Number :'))
#initialize an array
arr = []
#find prime numbers
for i in range(2,Number):
    flag = 0
    for j in range(2,i):
        if i % j == 0:
            flag = 1
    #append prime numbers to array
    if flag == 0:
        arr.append(i)
#possible combinations
flag = 0
for i in range(len(arr)):
    for j in range(i+1,len(arr)):
        #if condition is True Print numbers
        if(arr[i] + arr[j] == Number):
            flag = 1
            print(str(arr[i]) + " and " + str(arr[j]) + ' are prime numbers when added gives ' + str(Number))
            break
if(flag == 0):
print('No Prime numbers can give sum of ' + str(Number))
Output:
Enter the Number :30
7 and 23 are prime numbers when added gives 30
11 and 19 are prime numbers when added gives 30
13 and 17 are prime numbers when added gives 30