Count prime numbers up to N that can be represented as a sum of two prime numbers
Given a positive integer N, the task is to find the count of prime numbers less than or equal to N that can be represented as a sum of two prime numbers.
Examples:
Input: N = 6
Output: 1
Explanation:
5 is the only prime number over the range [1, 6] that can be represented as sum of 2 prime numbers i.e., 5 = 2 + 3, where 2, 3 and 5 are all primes.
Therefore, the count is 2.
Input: N = 14
Output: 3
Naive Approach: The simplest approach to solve the given problem is to consider all possible pairs (i, j) over the range [1, N] and if i and j are prime numbers and (i + j) lies in the range [1, N] then increment the count of prime numbers. After checking for all possible pairs, print the value of the total count obtained.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the following observation:
- Apart from 2, all the prime numbers are odd
- It is not possible to represent a prime number(which is odd) to be represented as a sum of two odd prime numbers, so one of the two prime numbers should be 2.
- So for a prime number X to be a sum of two prime numbers, X – 2 must also be prime.
Follow the steps below to solve the problem:
- Initialize an array, say prime[] of size 105, and populate all the prime numbers till 105 using the Sieve Of Eratosthenes.
- Initialize an auxiliary array dp[] of the size (N + 1) where dp[i] is the count of prime numbers less than or equal to i that can be represented as a sum of 2 prime numbers.
- Iterate over the range [2, N] using the variable i and perform the following steps:
- Update the value of dp[i – 1] as the sum of dp[i – 1] and dp[i].
- Check if prime[i] and prime[i – 2] is true, then increment the value of dp[i] by 1.
- After completing the above steps, print the value of dp[N] as the resultant count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes(
int n, bool prime[])
{
prime[0] = 0;
prime[1] = 0;
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p;
i <= n; i += p) {
prime[i] = false ;
}
}
}
}
void countPrime( int n)
{
bool prime[n + 1];
memset (prime, true , sizeof (prime));
SieveOfEratosthenes(n, prime);
int dp[n + 1];
memset (dp, 0, sizeof (dp));
dp[1] = 0;
for ( int i = 2; i <= n; i++) {
dp[i] += dp[i - 1];
if (prime[i] == 1
&& prime[i - 2] == 1) {
dp[i]++;
}
}
cout << dp[n];
}
int main()
{
int N = 6;
countPrime(N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static void SieveOfEratosthenes( int n, boolean prime[])
{
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
{
prime[i] = false ;
}
}
}
}
static void countPrime( int n)
{
boolean prime[] = new boolean [n + 1 ];
for ( int i = 0 ; i < prime.length; i++)
{
prime[i] = true ;
}
SieveOfEratosthenes(n, prime);
int dp[] = new int [n + 1 ];
for ( int i = 0 ; i < dp.length; i++)
{
dp[i] = 0 ;
}
dp[ 1 ] = 0 ;
for ( int i = 2 ; i <= n; i++)
{
dp[i] += dp[i - 1 ];
if (prime[i] == true &&
prime[i - 2 ] == true )
{
dp[i]++;
}
}
System.out.print(dp[n]);
}
public static void main(String[] args)
{
int N = 6 ;
countPrime(N);
}
}
|
Python3
def SieveOfEratosthenes(n, prime):
prime[ 0 ] = 0
prime[ 1 ] = 0
p = 2
while p * p < = n:
if (prime[p] = = True ):
for i in range (p * p, n + 1 , p):
prime[i] = False
p + = 1
def countPrime(n):
prime = [ True ] * (n + 1 )
SieveOfEratosthenes(n, prime)
dp = [ 0 ] * (n + 1 )
dp[ 1 ] = 0
for i in range ( 2 , n + 1 ):
dp[i] + = dp[i - 1 ]
if (prime[i] = = 1 and prime[i - 2 ] = = 1 ):
dp[i] + = 1
print (dp[n])
if __name__ = = "__main__" :
N = 6
countPrime(N)
|
C#
using System;
class GFG{
static void SieveOfEratosthenes( int n, bool [] prime)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
{
prime[i] = false ;
}
}
}
}
static void countPrime( int n)
{
bool [] prime = new bool [n + 1];
for ( int i = 0; i < prime.Length; i++)
{
prime[i] = true ;
}
SieveOfEratosthenes(n, prime);
int [] dp = new int [n + 1];
for ( int i = 0; i < dp.Length; i++)
{
dp[i] = 0;
}
dp[1] = 0;
for ( int i = 2; i <= n; i++)
{
dp[i] += dp[i - 1];
if (prime[i] == true &&
prime[i - 2] == true )
{
dp[i]++;
}
}
Console.Write(dp[n]);
}
static void Main()
{
int N = 6;
countPrime(N);
}
}
|
Javascript
<script>
function SieveOfEratosthenes(n, prime)
{
prime[0] = 0;
prime[1] = 0;
for ( var p = 2; p * p <= n; p++)
{
if (prime[p] == Boolean( true ))
{
for ( var i = p * p;i <= n; i += p)
{
prime[i] = Boolean( false );
}
}
}
}
function countPrime(n)
{
var prime = new Array(n + 1);
var x = new Boolean( true );
prime.fill(x);
SieveOfEratosthenes(n, prime);
var dp = new Array(n + 1);
dp.fill(0);
dp[1] = 0;
for ( var i = 2; i <= n; i++)
{
dp[i] += dp[i - 1];
if (prime[i] == Boolean( true ) &&
prime[i - 2] == Boolean( true ))
{
dp[i]++;
}
}
document.write(dp[n]);
}
var n = 6;
countPrime(n);
</script>
|
Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(N)
Approach:
Here is the code of above algorithm:
C++
#include <cmath>
#include <iostream>
#include <unordered_set>
using namespace std;
bool isPrime( int n)
{
if (n <= 1) {
return false ;
}
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
void countPrime( int n)
{
unordered_set< int > primes;
int count = 0;
for ( int i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.insert(i);
if (primes.count(i - 2) > 0) {
count++;
}
}
}
cout << count << endl;
}
int main()
{
int N = 6;
countPrime(N);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class Main {
public static boolean isPrime( int n) {
if (n <= 1 ) {
return false ;
}
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
if (n % i == 0 ) {
return false ;
}
}
return true ;
}
public static void countPrime( int n) {
Set<Integer> primes = new HashSet<>();
int count = 0 ;
for ( int i = 2 ; i <= n; i++) {
if (isPrime(i)) {
primes.add(i);
if (primes.contains(i - 2 )) {
count++;
}
}
}
System.out.println(count);
}
public static void main(String[] args) {
int N = 6 ;
countPrime(N);
}
}
|
Python3
def isPrime(n):
if n < = 1 :
return False
for i in range ( 2 , int (n * * 0.5 ) + 1 ):
if n % i = = 0 :
return False
return True
def countPrime(n):
primes = set ()
count = 0
for i in range ( 2 , n + 1 ):
if isPrime(i):
primes.add(i)
if (i - 2 ) in primes:
count + = 1
print (count)
if __name__ = = "__main__" :
N = 6
countPrime(N)
|
C#
using System;
using System.Collections.Generic;
namespace PrimeCount
{
class GFG
{
static bool IsPrime( int n)
{
if (n <= 1)
{
return false ;
}
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
static void CountPrime( int n)
{
HashSet< int > primes = new HashSet< int >();
int count = 0;
for ( int i = 2; i <= n; i++)
{
if (IsPrime(i))
{
primes.Add(i);
if (primes.Contains(i - 2))
{
count++;
}
}
}
Console.WriteLine(count);
}
static void Main( string [] args)
{
int N = 6;
CountPrime(N);
}
}
}
|
Javascript
function isPrime(n) {
if (n <= 1) {
return false ;
}
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
return false ;
}
}
return true ;
}
function countPrime(n) {
const primes = new Set();
let count = 0;
for (let i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.add(i);
if (primes.has(i - 2)) {
count++;
}
}
}
console.log(count);
}
const N = 6;
countPrime(N);
|
Time Complexity: O(n^(3/2))
Auxiliary Space: O(sqrt(n))
Last Updated :
16 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...