Given an integer ‘sum’ (less than 10^8), the task is to find a pair of prime numbers whose sum is equal to the given ‘sum’
Out of all the possible pairs, the absolute difference between the chosen pair must be minimum.
If the ‘sum’ cannot be represented as a sum of two prime numbers then print “Cannot be represented as sum of two primes”.
Input : Sum = 1002 Output : Primes: 499 503 Explanation 1002 can be represented as sum of many prime number pairs such as 499 503 479 523 461 541 439 563 433 569 431 571 409 593 401 601... But 499 and 503 is the only pair which has minimum difference Input :Sum = 2002 Output : Primes: 983 1019
- We will create a sieve of Eratosthenes which will store all the prime numbers and check whether a number is prime or not in O(1) time.
- Now, to find two prime numbers with sum equal to the given variable, ‘sum’. We will start a loop from sum/2 to 1 (to minimize the absolute difference) and check whether the loop counter ‘i’ and ‘sum-i’ are both prime.
- If they are prime then we will print them and break out of the loop.
- If the ‘sum’ cannot be represented as a sum of two prime numbers then we will print “Cannot be represented as sum of two primes”.
Below is the implementation of the above solution:
# Python 3 implementation of the above approach
from math import sqrt
# stores whether a number is prime or not
# create the sieve of eratosthenes
MAX = 1000001
# Create a boolean array “prime[0..n]” and
# initialize all entries it as true. A value
# in prime[i] will finally be false if i is
# Not a prime, else true.
prime = [True for i in range(MAX + 1)]
prime = False
for p in range(2, int(sqrt(MAX)) + 1, 1):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
# as non-prime
for i in range(p * 2, MAX + 1, p):
prime[i] = False
# find the two prime numbers with minimum
# difference and whose sum is equal to
# variable sum
# start from sum/2 such that difference
# between i and sum-i will be minimum
# create the sieve
prime = SieveOfEratosthenes()
i = int(sum / 2)
while(i > 1):
# if both ‘i’ and ‘sum – i’ are prime
# then print them and break the loop
if (prime[i] and prime[sum – i]):
print(i, (sum – i))
i -= 1
# if there is no prime
print(“Cannot be represented as sum”,
“of two primes”)
# Driver code
if __name__ == ‘__main__’:
sum = 1002
# find the primes
# This code is contributed by
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Pair with minimum absolute difference after solving each query
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Minimum absolute difference of a number and its closest prime
- Co-prime pair with given sum minimum difference
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Minimum absolute difference between N and a power of 2
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Queries for maximum difference between prime numbers in given ranges
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Queries for the difference between the count of composite and prime numbers in a given range
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Minimum and Maximum prime numbers in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.