Find two prime numbers with given sum Last Updated : 04 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given an even number n (greater than 2), the task is to find two prime numbers whose sum will be equal to the given number. Note: There are several combinations possible, find only the pair whose minimum value is the smallest among all the minimum values of pairs and print the minimum element first. The solution will always exist according to Goldbach’s conjecture.ExamplesInput: n = 74Output: 3 71Explanation: There are several possibilities like 37 37. But the minimum value of this pair is 3 which is the smallest among all possible minimum values of all the pairs.Input: n = 4Output: 2 2Explaination: This is the only possible partitioning of 4.Approach:The idea is to use the Sieve of Eratosthenes to find all prime numbers up to n. Then, we iterate through numbers from 2 to n/2, checking if both the number and its complement (i.e., n - number) are prime. The first valid pair of primes whose sum equals n is returned, ensuring the smallest prime pair is selected. C++ // C++ code to find two primes whose sum // equals given even number #include <bits/stdc++.h> using namespace std; // Function to generate primes up to n // using Sieve of Eratosthenes vector<bool> sieve(int n) { // Initialize all as prime vector<bool> isPrime(n + 1, true); // 0 and 1 are not primes isPrime[0] = isPrime[1] = false; // Mark non-primes using multiples of each prime for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return isPrime; } // Function to find two primes whose sum equals n vector<int> primeDivision(int n) { // Get all primes up to n vector<bool> isPrime = sieve(n); // Iterate to find the smallest pair for (int i = 2; i <= n / 2; i++) { if (isPrime[i] && isPrime[n - i]) { return {i, n - i}; } } // Return empty if no pair found (won't occur) return {}; } int main() { int n = 74; vector<int> result = primeDivision(n); cout << result[0] << " " << result[1]; return 0; } Java // Java code to find two primes whose sum // equals given even number import java.util.*; class GfG { // Function to generate primes up to n // using Sieve of Eratosthenes static boolean[] sieve(int n) { // Initialize all as prime boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); // 0 and 1 are not primes isPrime[0] = isPrime[1] = false; // Mark non-primes using multiples of each prime for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return isPrime; } // Function to find two primes whose sum equals n static List<Integer> primeDivision(int n) { // Get all primes up to n boolean[] isPrime = sieve(n); // Iterate to find the smallest pair for (int i = 2; i <= n / 2; i++) { if (isPrime[i] && isPrime[n - i]) { return Arrays.asList(i, n - i); } } // Return empty list if no pair found (won't occur) return new ArrayList<>(); } public static void main(String[] args) { int n = 74; List<Integer> result = primeDivision(n); System.out.print(result.get(0) + " " + result.get(1)); } } Python # Python code to find two primes whose sum # equals given even number # Function to generate primes up to n # using Sieve of Eratosthenes def sieve(n): # Initialize all as prime isPrime = [True] * (n + 1) # 0 and 1 are not primes isPrime[0] = isPrime[1] = False # Mark non-primes using multiples of each prime for i in range(2, int(n**0.5) + 1): if isPrime[i]: for j in range(i * i, n + 1, i): isPrime[j] = False return isPrime # Function to find two primes whose sum equals n def primeDivision(n): # Get all primes up to n isPrime = sieve(n) # Iterate to find the smallest pair for i in range(2, n // 2 + 1): if isPrime[i] and isPrime[n - i]: return [i, n - i] # Return empty list if no pair found (won't occur) return [] if __name__ == '__main__': n = 74 result = primeDivision(n) print(result[0], result[1]) C# // C# code to find two primes whose sum // equals given even number using System; using System.Collections.Generic; class GfG { // Function to generate primes up to n // using Sieve of Eratosthenes static bool[] Sieve(int n) { // Initialize all as prime bool[] isPrime = new bool[n + 1]; Array.Fill(isPrime, true); // 0 and 1 are not primes isPrime[0] = isPrime[1] = false; // Mark non-primes using multiples of each prime for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return isPrime; } // Function to find two primes whose sum equals n static List<int> PrimeDivision(int n) { // Get all primes up to n bool[] isPrime = Sieve(n); // Iterate to find the smallest pair for (int i = 2; i <= n / 2; i++) { if (isPrime[i] && isPrime[n - i]) { return new List<int> { i, n - i }; } } // Return empty list if no pair found (won't occur) return new List<int>(); } static void Main(string[] args) { int n = 74; List<int> result = PrimeDivision(n); Console.WriteLine(result[0] + " " + result[1]); } } JavaScript // Javascript code to find two primes whose sum // equals given even number // Function to generate primes up to n // using Sieve of Eratosthenes function sieve(n) { // Initialize all as prime let isPrime = Array(n + 1).fill(true); // 0 and 1 are not primes isPrime[0] = isPrime[1] = false; // Mark non-primes using multiples of each prime for (let i = 2; i * i <= n; i++) { if (isPrime[i]) { for (let j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return isPrime; } // Function to find two primes whose sum equals n function primeDivision(n) { // Get all primes up to n const isPrime = sieve(n); // Iterate to find the smallest pair for (let i = 2; i <= n / 2; i++) { if (isPrime[i] && isPrime[n - i]) { return [i, n - i]; } } // Return empty array if no pair found (won't occur) return []; } const n = 74; const result = primeDivision(n); console.log(result[0], result[1]); Output3 71Time Complexity: O(n * log(log(n))), due to the Sieve of Eratosthenes, followed by a linear scan to find the pair.Auxiliary Space: O(n), for the array used to store prime numbers up to n. Comment More infoAdvertise with us Next Article Find two prime numbers with given sum R Rakesh Kumar Improve Article Tags : Mathematical DSA Amazon Yahoo Zoho sieve Prime Number +3 More Practice Tags : AmazonYahooZohoMathematicalPrime Numbersieve +2 More Similar Reads Find three prime numbers with given sum Given an integer N, the task is to find three prime numbers X, Y, and Z such that the sum of these three numbers is equal to N i.e. X + Y + Z = N. Examples: Input: N = 20 Output: 2 5 13 Input: N = 34 Output: 2 3 29 Approach: Generate prime numbers using Sieve of EratosthenesStart from the first prim 9 min read Sum of the first N Prime numbers Given an integer 'n', the task is to find the sum of first 'n' prime numbers. First few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, ...... Examples: Input: N = 4Output: 172, 3, 5, 7 are first 4 prime numbers so their sum is equal to 17Input: N = 40Output: 3087 Approach: Create a sieve which w 7 min read Find the sum of prime numbers in the Kth array Given K arrays where the first array contains the first prime number, the second array contains the next 2 primes and the third array contains the next 3 primes and so on. The task is to find the sum of primes in the Kth array.Examples: Input: K = 3 Output: 31 arr1[] = {2} arr[] = {3, 5} arr[] = {7, 9 min read Prime numbers after prime P with sum S Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.Examples : Input : N = 2, P = 7, S = 28 Output : 11 17 Explanation : 11 and 17 are primes after prime 7 and (11 + 17 = 28) Input : N = 3, P = 2, S = 23 Output : 3 7 13 5 7 11 Explanati 13 min read Count pairs with sum as a prime number and less than n Given a positive integer n, count distinct number of pairs (x, y) that satisfy following conditions : (x + y) is a prime number.(x + y) < nx != y1 <= x, y Examples: Input : n = 6 Output : 3 prime pairs whose sum is less than 6 are: (1,2), (1,4), (2,3) Input : 12 Output : 11 prime pairs whose s 10 min read Sum of all numbers up to N that are co-prime with N Given an integer N, the task is to find the sum of all numbers in the range [1, N] that are co-prime with the given number N. Examples: Input: N = 5Output: 10Explanation:Numbers which are co-prime with 5 are {1, 2, 3, 4}.Therefore, the sum is given by 1 + 2 + 3 + 4 = 10. Input: N = 6Output: 5Explana 10 min read Sum of prime numbers without odd prime digits Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosth 8 min read Sum of every K'th prime number in an array Given an array of integers (less than 10^6), the task is to find the sum of all the prime numbers which appear after every (k-1) prime number i.e. every K'th prime number in the array. Examples: Input : Array : 2, 3, 5, 7, 11 ; n=5; k=2 Output : Sum = 10 Explanation: All the elements of the array ar 7 min read Sum of every Kâth prime number in an array Given an integer k and an array of integers arr (less than 10^6), the task is to find the sum of every kâth prime number in the array. Examples: Input: arr[] = {2, 3, 5, 7, 11}, k = 2 Output: 10 All the elements of the array are prime. So, the prime numbers after every K (i.e. 2) interval are 3, 7 a 9 min read Maximum Primes whose sum is equal to given N Given a positive integer N > 1. Find the maximum count of prime numbers whose sum is equal to the given N.Examples: Input : N = 5 Output : 2 Explanation : 2 and 3 are two prime numbers whose sum is 5.Input : N = 6 Output :3 Explanation : 2, 2, 2 are three prime numbers whose sum is 6. For the max 3 min read Like