Find a sequence of N prime numbers whose sum is a composite number Last Updated : 31 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 a number of times will give an even sum.When N is odd then we can print 2 and any other N - 1 prime 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 print the 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 # Print the sequence while (cnt < n): print(v[i],end=" ") i += 1 cnt += 1 # Driver code n = 6 SieveOfEratosthenes(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 */ JavaScript <script> // Javascript implementation // of the approach var MAXN = 100000; // To store prime numbers var v = []; // Function to find and store // all the primes <= n function 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. var prime = Array(n + 1).fill(true); var p; for (p = 2; p * p <= n; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { var i; // 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 = p * p; i <= n; i += p) prime[i] = false; } } // Store all the prime numbers for (p = 2; p <= n; p++) if (prime[p]) v.push(p); } // Function to print // the required sequence function 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 var i = (n % 2 == 0) ? 1 : 0; var cnt = 0; // Print the sequence while (cnt < n) { document.write(v[i] + " "); i++; cnt++; } } // Driver code n = 6; SieveOfEratosthenes(MAXN); GetSequence(n); </script> Output: 3 5 7 11 13 17 Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Check if a number can be written as a sum of 'k' prime numbers S souradeep Follow Improve Article Tags : Mathematical DSA Prime Number Constructive Algorithms Practice Tags : MathematicalPrime Number Similar Reads Count of all subsequence whose product is a Composite number Given an array arr[], the task is to find the number of non-empty subsequences from the given array such that the product of subsequence is a composite number.Example: Input: arr[] = {2, 3, 4} Output: 5 Explanation: There are 5 subsequences whose product is composite number {4}, {2, 3}, {2, 4}, {3, 6 min read R program to find prime and composite numbers in an interval A natural number (1, 2, 3, 4, 5 and so on) is called a prime number if it is greater than 1 and cannot be written as the product of two smaller natural numbers. The numbers greater than 1 that are not prime are called composite numbers. A composite number is a positive integer that can be formed by 7 min read Check if a number can be written as a sum of 'k' prime numbers Given two numbers N and K. We need to find out if 'N' can be written as sum of 'K' prime numbers. Given N <= 10^9 Examples : Input : N = 10 K = 2 Output : Yes 10 can be written as 5 + 5 Input : N = 2 K = 2 Output : NoRecommended PracticePrimes sumTry It! The idea is to use Goldbach's conjecture w 8 min read Sum of numbers in a range [L, R] whose count of divisors is prime Given Q queries where each query consists of an integer range [L, R], the task is to find the sum of the integers from the given range whose count of divisors is prime.Examples: Input: Q[][] = {{2, 4}} Output: 9 All the numbers in the range have only 2 divisors which is prime. (2 + 3 + 4) = 9.Input: 11 min read Sum of all composite numbers lying in the range [L, R] for Q queries Given Q queries in the form of 2D array arr[][] whose every row consists of two numbers L and R which denotes the range [L, R], the task is to find the sum of all Composite Numbers lying in range [L, R]. Input: arr[][] = {{10, 13}, {12, 21}} Output: 22 116 Explanation: From 10 to 13 only 10 and 12 i 10 min read Check if a prime number can be expressed as sum of two Prime Numbers Given a number n, the task is to check if it is possible to express n as the sum of two prime numbers, a and b. If such pair does not exist, return [-1, -1].Note: If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then [a, b] is considered as our an 9 min read Like