Count primes that can be expressed as sum of two consecutive primes and 1

Given a number N. The task is to count the number of prime numbers from 2 to N that can be expressed as a sum of two consecutive primes and 1.

Examples:

Input: N = 27
Output: 2
13 = 5 + 7 + 1 and 19 = 7 + 11 + 1 are the required prime numbers.

Input: N = 34
Output: 3
13 = 5 + 7 + 1, 19 = 7 + 11 + 1 and 31 = 13 + 17 + 1.

Approach: An efficient approach is to find all the primes numbers up to N using Sieve of Eratosthenes and place all the prime numbers in a vector. Now, run a simple loop and add two consecutive primes and 1 then check if this sum is also a prime. If it is then increment the count.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define N 100005
  
// To check if a number is prime or not
bool isprime[N];
  
// To store possible numbers
bool can[N];
  
// Function to return all prime numbers
vector<int> SieveOfEratosthenes()
{
  
    memset(isprime, true, sizeof(isprime));
  
    for (int p = 2; p * p < N; p++) {
  
        // If prime[p] is not changed, then it is a prime
        if (isprime[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)
                isprime[i] = false;
        }
    }
  
    vector<int> primes;
    for (int i = 2; i < N; i++)
        if (isprime[i])
            primes.push_back(i);
  
    return primes;
}
  
// Function to count all possible prime numbers that can be
// expressed as the sum of two consecutive primes and one
int Prime_Numbers(int n)
{
    vector<int> primes = SieveOfEratosthenes();
  
    // All possible prime numbers below N
    for (int i = 0; i < (int)(primes.size()) - 1; i++)
        if (primes[i] + primes[i + 1] + 1 < N)
            can[primes[i] + primes[i + 1] + 1] = true;
  
    int ans = 0;
    for (int i = 2; i <= n; i++) {
        if (can[i] and isprime[i]) {
            ans++;
        }
    }
  
    return ans;
}
  
// Drive code
int main()
{
    int n = 50;
    cout << Prime_Numbers(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
import java.util.*;
  
class GfG 
{
  
static int N = 100005
  
// To check if a number is prime or not 
static boolean isprime[] = new boolean[N]; 
  
// To store possible numbers 
static boolean can[] = new boolean[N]; 
  
// Function to return all prime numbers 
static ArrayList<Integer>SieveOfEratosthenes() 
      
    for(int a = 0 ; a < isprime.length; a++)
    {
        isprime[a] = true;
    }
    for (int p = 2; p * p < N; p++) 
    
  
        // If prime[p] is not changed, then it is a prime 
        if (isprime[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) 
                isprime[i] = false
        
    
  
    ArrayList<Integer> primes = new ArrayList<Integer> (); 
    for (int i = 2; i < N; i++) 
        if (isprime[i]) 
            primes.add(i); 
  
    return primes; 
  
// Function to count all possible prime numbers that can be 
// expressed as the sum of two consecutive primes and one 
static int Prime_Numbers(int n) 
    ArrayList<Integer> primes = SieveOfEratosthenes(); 
  
    // All possible prime numbers below N 
    for (int i = 0; i < (int)(primes.size()) - 1; i++) 
        if (primes.get(i) + primes.get(i + 1) + 1 < N) 
            can[primes.get(i) + primes.get(i + 1) + 1] = true
  
    int ans = 0
    for (int i = 2; i <= n; i++) 
    
        if (can[i] && isprime[i] == true
        
            ans++; 
        
    
  
    return ans; 
  
// Drive code 
public static void main(String[] args) 
    int n = 50
    System.out.println(Prime_Numbers(n)); 
}
  
// This code is contributed by 
// Prerna Saini.

chevron_right


Output:

5


My Personal Notes arrow_drop_up