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 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++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100000
vector< int > v;
void SieveOfEratosthenes( int n)
{
bool prime[n + 1];
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p <= n; p++)
if (prime[p])
v.push_back(p);
}
void GetSequence( int n)
{
int i = (n % 2 == 0) ? 1 : 0;
int cnt = 0;
while (cnt < n) {
cout << v[i] << " " ;
i++;
cnt++;
}
}
int main()
{
int n = 6;
SieveOfEratosthenes(MAXN);
GetSequence(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAXN = 100000 ;
static Vector<Integer> v = new Vector<Integer>();
static void SieveOfEratosthenes( int n)
{
boolean [] prime = new boolean [n + 1 ];
Arrays.fill(prime, true );
for ( int p = 2 ; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
for ( int p = 2 ; p <= n; p++)
if (prime[p])
v.add(p);
}
static void GetSequence( int n)
{
int i = (n % 2 == 0 ) ? 1 : 0 ;
int cnt = 0 ;
while (cnt < n)
{
System.out.print(v.get(i) + " " );
i++;
cnt++;
}
}
public static void main(String[] args)
{
int n = 6 ;
SieveOfEratosthenes(MAXN);
GetSequence(n);
}
}
|
python
MAXN = 100000
v = []
def SieveOfEratosthenes(n):
prime = [ True for i in range (n + 1 )]
for p in range ( 2 ,n + 1 ):
if (prime[p] = = True ):
for i in range ( 2 * p, n + 1 , p):
prime[i] = False
for p in range ( 2 , n + 1 ):
if (prime[p]):
v.append(p)
def GetSequence(n):
if n % 2 = = 0 :
i = 1
else :
i = 0
cnt = 0
while (cnt < n):
print (v[i],end = " " )
i + = 1
cnt + = 1
n = 6
SieveOfEratosthenes(MAXN)
GetSequence(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAXN = 100000;
static List< int > v = new List< int >();
static void SieveOfEratosthenes( int n)
{
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] == true )
{
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p <= n; p++)
if (prime[p])
v.Add(p);
}
static void GetSequence( int n)
{
int i = (n % 2 == 0) ? 1 : 0;
int cnt = 0;
while (cnt < n)
{
Console.Write(v[i] + " " );
i++;
cnt++;
}
}
public static void Main(String[] args)
{
int n = 6;
SieveOfEratosthenes(MAXN);
GetSequence(n);
}
}
|
Javascript
<script>
var MAXN = 100000;
var v = [];
function SieveOfEratosthenes(n)
{
var prime = Array(n + 1).fill( true );
var p;
for (p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
var i;
for (i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
for (p = 2; p <= n; p++)
if (prime[p])
v.push(p);
}
function GetSequence(n)
{
var i = (n % 2 == 0) ? 1 : 0;
var cnt = 0;
while (cnt < n) {
document.write(v[i] + " " );
i++;
cnt++;
}
}
n = 6;
SieveOfEratosthenes(MAXN);
GetSequence(n);
</script>
|
Auxiliary Space: O(n)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...