Count prime numbers that can be expressed as sum of consecutive prime numbers
Last Updated :
26 Apr, 2021
Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes.
Examples:
Input: N = 45
Output: 3
Explanation:
Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers:
- 5 = 2 + 3
- 17 = 2 + 3 + 5 + 7
- 41 = 2 + 3 + 5 + 7 + 11 + 13
Therefore, the count is 3.
Input: N = 4
Output: 0
Approach: The idea is to use the Primality Test Algorithm. Using this, all primes not exceeding N can be found. After that, each number that can be expressed as consecutive primes can be found. Follow the steps below to solve the problem:
- Traverse through each number from 1 to N, checking if it is a prime, and stored it in a vector.
- Sort all the stored prime numbers in the vector.
- Let there be X primes present in the vector. Initialize sum as the smallest prime found i.e., element at index 0 in the vector.
- Iterate over the range [1, X – 1] and add each element to the sum.
- After adding, check if the sum is a prime or not and the sum is less than N or not. If it found to be true, then increment the counter. Otherwise, if the sum becomes greater than N, break the loop.
- After all the above steps, print the count of prime numbers stored on the counter.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isprm( int n)
{
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 || n % 3 == 0)
return 0;
for ( int i = 5;
i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0)
return 0;
}
return 1;
}
int countprime( int n)
{
int count = 0;
vector< int > primevector;
for ( int i = 2; i <= n; i++) {
if (isprm(i) == 1) {
primevector.push_back(i);
}
}
int sum = primevector[0];
for ( int i = 1;
i < primevector.size(); i++) {
sum += primevector[i];
if (sum > n)
break ;
if (isprm(sum) == 1) {
count++;
}
}
return count;
}
int main()
{
int N = 45;
cout << countprime(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int isprm( int n)
{
if (n <= 1 )
return 0 ;
if (n <= 3 )
return 1 ;
if (n % 2 == 0 ||
n % 3 == 0 )
return 0 ;
for ( int i = 5 ; i * i <= n;
i = i + 6 )
{
if (n % i == 0 ||
n % (i + 2 ) == 0 )
return 0 ;
}
return 1 ;
}
static int countprime( int n)
{
int count = 0 ;
Vector<Integer> primevector =
new Vector<>();
for ( int i = 2 ; i <= n; i++)
{
if (isprm(i) == 1 )
{
primevector.add(i);
}
}
int sum = primevector.elementAt( 0 );
for ( int i = 1 ;
i < primevector.size(); i++)
{
sum += primevector.elementAt(i);
if (sum > n)
break ;
if (isprm(sum) == 1 )
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 45 ;
System.out.print(countprime(N));
}
}
|
Python3
def isprm(n):
if (n < = 1 ):
return 0
if (n < = 3 ):
return 1
if (n % 2 = = 0 or n % 3 = = 0 ):
return 0
i = 5
while (i * i < = n):
if (n % i = = 0 or
n % (i + 2 ) = = 0 ):
return 0
i = i + 6
return 1
def countprime(n):
count = 0
primevector = []
for i in range ( 2 , n + 1 ):
if (isprm(i) = = 1 ):
primevector.append(i)
sum = primevector[ 0 ]
for i in range ( 1 , len (primevector)):
sum + = primevector[i]
if ( sum > n):
break
if (isprm( sum ) = = 1 ):
count + = 1
return count
N = 45
print (countprime(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int isprm( int n)
{
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 ||
n % 3 == 0)
return 0;
for ( int i = 5; i * i <= n;
i = i + 6)
{
if (n % i == 0 ||
n % (i + 2) == 0)
return 0;
}
return 1;
}
static int countprime( int n)
{
int count = 0;
List< int > primevector = new List< int >();
for ( int i = 2; i <= n; i++)
{
if (isprm(i) == 1)
{
primevector.Add(i);
}
}
int sum = primevector[0];
for ( int i = 1; i < primevector.Count; i++)
{
sum += primevector[i];
if (sum > n)
break ;
if (isprm(sum) == 1)
{
count++;
}
}
return count;
}
public static void Main(String[] args)
{
int N = 45;
Console.Write(countprime(N));
}
}
|
Javascript
<script>
function isprm(n)
{
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 || n % 3 == 0)
return 0;
for ( var i = 5;
i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0)
return 0;
}
return 1;
}
function countprime(n)
{
var count = 0;
var primevector = [];
for ( var i = 2; i <= n; i++) {
if (isprm(i) == 1) {
primevector.push(i);
}
}
var sum = primevector[0];
for ( var i = 1;
i < primevector.length; i++) {
sum += primevector[i];
if (sum > n)
break ;
if (isprm(sum) == 1) {
count++;
}
}
return count;
}
var N = 45;
document.write( countprime(N));
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(?N)
Efficient Approach: The above approach can be optimized by precomputing the prime numbers up to N using the Sieve of Eratosthenes.
Time Complexity: O(N*log(logN))
Auxiliary Space: O(log(logN))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...