Find a sequence of N prime numbers whose sum is a composite number
Given an integer N and the task is to find a sequence of N prime numbers whose sum is a composite number.
Examples:
Input: N = 5
Output: 2 3 5 7 11
2 + 3 + 5 + 7 + 11 = 28 which is composite.
Input: N = 6
Output: 3 5 7 11 13 17
Approach: The sum of two prime numbers is always even which is composite as they are odd numbers except 2. There are two cases now,
- When N is even then we can print any N prime numbers except 2 and their sum will always be even i.e. odd numbers when added even number of times will give an even sum.
- When N is odd then we can print 2 and any other N – 1 primes to make sure that the sum is even. Since, N – 1 is even so the sum will be even for any primes except 2 then we add 2 as the Nth number to make sure that the sum remains even.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MAXN 100000 // To store prime numbers vector<int> v; // Function to find and store // all the primes <= n void SieveOfEratosthenes(int n) { // 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. bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Store all the prime numbers for (int p = 2; p <= n; p++) if (prime[p]) v.push_back(p); } // Function to print the required sequence void GetSequence(int n) { // If n is even then we do not include 2 // and start the sequence from the 2nd // smallest prime else we include 2 int i = (n % 2 == 0) ? 1 : 0; int cnt = 0; // Print the sequence while (cnt < n) { cout << v[i] << " "; i++; cnt++; } } // Driver code int main() { int n = 6; SieveOfEratosthenes(MAXN); GetSequence(n); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG { static int MAXN = 100000; // To store prime numbers static Vector<Integer> v = new Vector<Integer>(); // Function to find and store // all the primes <= n static void SieveOfEratosthenes(int n) { // 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. boolean[] prime = new boolean[n + 1]; Arrays.fill(prime,true); for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Store all the prime numbers for (int p = 2; p <= n; p++) if (prime[p]) v.add(p); } // Function to print the required sequence static void GetSequence(int n) { // If n is even then we do not include 2 // and start the sequence from the 2nd // smallest prime else we include 2 int i = (n % 2 == 0) ? 1 : 0; int cnt = 0; // Print the sequence while (cnt < n) { System.out.print(v.get(i) + " "); i++; cnt++; } } // Driver code public static void main(String[] args) { int n = 6; SieveOfEratosthenes(MAXN); GetSequence(n); } } // This code is contributed by Princi Singh |
python
# Python3 implementation of the approach MAXN=100000 # To store prime numbers v=[] # Function to find and store # all the primes <= n def SieveOfEratosthenes(n): # 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(n + 1)] for p in range(2,n+1): # If prime[p] is not changed, then it is a prime if (prime[p] == True): # Update all multiples of p greater than or # equal to the square of it # numbers which are multiple of p and are # less than p^2 are already been marked. for i in range(2 * p, n + 1, p): prime[i] = False # Store all the prime numbers for p in range(2, n + 1): if (prime[p]): v.append(p) # Function to prthe required sequence def GetSequence(n): # If n is even then we do not include 2 # and start the sequence from the 2nd # smallest prime else we include 2 if n % 2 == 0: i = 1 else: i = 0 cnt = 0 # Prthe sequence while (cnt < n): print(v[i],end=" ") i += 1 cnt += 1 # Driver code n = 6SieveOfEratosthenes(MAXN) GetSequence(n) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { static int MAXN = 100000; // To store prime numbers static List<int> v = new List<int>(); // Function to find and store // all the primes <= n static void SieveOfEratosthenes(int n) { // 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. Boolean[] prime = new Boolean[n + 1]; for(int i = 0; i < n + 1; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Store all the prime numbers for (int p = 2; p <= n; p++) if (prime[p]) v.Add(p); } // Function to print the required sequence static void GetSequence(int n) { // If n is even then we do not include 2 // and start the sequence from the 2nd // smallest prime else we include 2 int i = (n % 2 == 0) ? 1 : 0; int cnt = 0; // Print the sequence while (cnt < n) { Console.Write(v[i] + " "); i++; cnt++; } } // Driver code public static void Main(String[] args) { int n = 6; SieveOfEratosthenes(MAXN); GetSequence(n); } } /* This code is contributed by PrinciRaj1992 */ |
3 5 7 11 13 17
Recommended Posts:
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- XOR of K largest prime and composite numbers from the given array
- Queries for the difference between the count of composite and prime numbers in a given range
- Find a range of composite numbers of given length
- Find the total number of composite factor for a given number
- Find the maximum number of composite summands of a number
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Find the XOR of first N Prime Numbers
- Find two prime numbers with given sum
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Find product of prime numbers between 1 to n
- Find the Product of first N Prime Numbers
- Find out the prime numbers in the form of A+nB or B+nA
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



