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:
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Co-prime pair with given sum minimum difference
- Pair with minimum absolute difference after solving each query
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Minimum absolute difference of a number and its closest prime
- Product of absolute difference of every pair in given Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Pair with min absolute difference and whose product is N+1 or N+2
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Split first N natural numbers into two sets with minimum absolute difference of their sums
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Prime numbers after prime P with sum S
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits 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 firstname.lastname@example.org. 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.