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.
Theory
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.
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.
Here we will check for all the numbers if they can be expressed as sum of two primes or not.
Algorithm
- Take number as input in n
- Initialize a variable flag as 0
- Iterate using for loop from value of i between (2, n/2)
- For each iteration Call a function sum_of_two_primes for value of i is it returns 1
- Call same function for value n-i and if it is also 1 then print i and n-i as answer increment the flag to 1
- If flag is 0 print not possible
- Create function sum_of_two_prime where check if passed number is prime return true else false
Python code
# 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
Insert the num: 15 15 can be expressed as the sum of 2 and 13
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
import math
def isPrime(n):
if n <= 1:
return 0
if n == 2:
return 1
if n % 2 == 0:
return 0
for i in range(3, int(math.sqrt(n))):
if i % n == 0:
return 0
return 1
primes = []
n = 15
for i in range(n):
if isPrime(i):
primes.append(i)
for i in range(len(primes)):
for j in range(i, len(primes)):
if primes[i] + primes[j] == n:
print(primes[i], primes[j], primes)
break
n=int(input(“enter the num”))
if n==0:
print(“given num is not a prime num: “,n)
lst=[]
for i in range(1,n+1):
flag=0
for j in range(1,i+1):
if i%j==0:
flag=flag+1
if flag==2:
lst.append(i)
print(lst)
a=0
for x in lst:
b=0
for y in lst:
if lst[a]+lst[b]==n:
print(lst[a],”+”,lst[b],”==”,n)
b=b+1
a=a+1
c = int(input(“Enter an even number greater than 2: “))
if c % 2 != 0:
print(“Please enter an even number.”)
else:
a_con = []
b_con = []
for a in range(2, c):
if all(a % i != 0 for i in range(2, a)):
b = c – a
if all(b % i != 0 for i in range(2, b)):
if a not in a_con and b not in b_con:
a_con.append(a)
b_con.append(b)
n1 = len(b_con)
n2 = n1/2
n3 = int(n2) + bool(n2%1)
if n1 == 1:
print(a_con[0], “+”, b_con[0], ” = “, c)
print(“so, there is one combination of prime numbers we can break “, c)
else:
print(“there are “, n3, ” possible combination of prime numbers we can break “, c)
for i in range(0, n3):
print(a_con[i], “+”, b_con[i], ” = “, c)
n=int(input())
a=[]
for i in range(2,n):
fact = 0
for j in range(2,i):
if i%j==0:
fact+=1
if fact==0:
a.append(i)
print(a)
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i]+a[j]==n :
print(“sum of two prime numbers”,a[i],a[j],’gives’,n)
num=int(input(“Enter the number “))
count=0
def isprime(n):
fact=0
for i in range(1,n+1):
if n%i==0:
fact+=1
if fact==2:
return True
else:
return False
for j in range(2,num//2+1):
if isprime(j) and isprime(num-j):
count+=1
print(j,”+”,num-j,”=”,num)
if count==0:
print(num,”can’t be represented as a sum of two prime numbers”)
num=int(input(“Enter the number “))
def nextprime(n):
fact=0
for i in range(1,n+1):
if n%i==0:
fact+=1
if fact==2:
return True
else:
return False
for j in range(2,num//2+1):
if isprime(j):
if isprime(num-j):
print(j,”+”,num-j,”=”,num)
val=int(input(“enter the value :”))
num=[]
for a in range(val):
if a > 1:
for x in range(2, a):
if a % x == 0:
break
else: num.append(a)
for x in range(len(num)):
for i in range(len(num)):
if num[x]+num[i]==val:
if num[x]<=num[i]:
print(num[x], " and ", num[i], " are prime numbers when added gives 30")
c=0
p=[]
z=int(input(‘enter the no :’))
for i in range(2,z):
c=0
y=i//2+1
for j in range(2,y):
if(i%j==0):
c=c+1
if(c==0):
print(i,p.append(i),’is prime no’)
# t=p.append(i)
print(p)
for i in p:
for j in p:
if(i+j==z):
print(i,j,’are the no.s whose sem equals to’,z)
By-Gunjan soni