Count prime numbers that can be expressed as sum of consecutive prime numbers
Last Updated :
24 Jun, 2024
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a
// number is prime or not
int isprm(int n)
{
// Base Case
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 || n % 3 == 0)
return 0;
// Iterate till [5, sqrt(N)] to
// detect primality of numbers
for (int i = 5;
i * i <= n; i = i + 6) {
// If N is divisible by i
// or i + 2
if (n % i == 0 || n % (i + 2) == 0)
return 0;
}
// Return 1 if N is prime
return 1;
}
// Function to count the prime numbers
// which can be expressed as sum of
// consecutive prime numbers
int countprime(int n)
{
// Initialize count as 0
int count = 0;
// Stores prime numbers
vector<int> primevector;
for (int i = 2; i <= n; i++) {
// If i is prime
if (isprm(i) == 1) {
primevector.push_back(i);
}
}
// Initialize the sum
int sum = primevector[0];
// Find all required primes upto N
for (int i = 1;
i < primevector.size(); i++) {
// Add it to the sum
sum += primevector[i];
if (sum > n)
break;
if (isprm(sum) == 1) {
count++;
}
}
// Return the final count
return count;
}
// Driver Code
int main()
{
// Given number N
int N = 45;
// Function Call
cout << countprime(N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to check if a
// number is prime or not
static int isprm(int n)
{
// Base Case
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 ||
n % 3 == 0)
return 0;
// Iterate till [5, Math.sqrt(N)]
// to detect primality of numbers
for (int i = 5; i * i <= n;
i = i + 6)
{
// If N is divisible by i
// or i + 2
if (n % i == 0 ||
n % (i + 2) == 0)
return 0;
}
// Return 1 if N is prime
return 1;
}
// Function to count the prime numbers
// which can be expressed as sum of
// consecutive prime numbers
static int countprime(int n)
{
// Initialize count as 0
int count = 0;
// Stores prime numbers
Vector<Integer> primevector =
new Vector<>();
for (int i = 2; i <= n; i++)
{
// If i is prime
if (isprm(i) == 1)
{
primevector.add(i);
}
}
// Initialize the sum
int sum = primevector.elementAt(0);
// Find all required primes upto N
for (int i = 1;
i < primevector.size(); i++)
{
// Add it to the sum
sum += primevector.elementAt(i);
if (sum > n)
break;
if (isprm(sum) == 1)
{
count++;
}
}
// Return the final count
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given number N
int N = 45;
// Function Call
System.out.print(countprime(N));
}
}
// This code is contributed by gauravrajput1
Python
# Python3 program for the above approach
# Function to check if a
# number is prime or not
def isprm(n):
# Base Case
if (n <= 1):
return 0
if (n <= 3):
return 1
if (n % 2 == 0 or n % 3 == 0):
return 0
# Iterate till [5, sqrt(N)] to
# detect primality of numbers
i = 5
while (i * i <= n):
# If N is divisible by i
# or i + 2
if (n % i == 0 or
n % (i + 2) == 0):
return 0
i = i + 6
# Return 1 if N is prime
return 1
# Function to count the prime numbers
# which can be expressed as sum of
# consecutive prime numbers
def countprime(n):
# Initialize count as 0
count = 0
# Stores prime numbers
primevector = []
for i in range(2, n + 1):
# If i is prime
if (isprm(i) == 1):
primevector.append(i)
# Initialize the sum
sum = primevector[0]
# Find all required primes upto N
for i in range(1, len(primevector)):
# Add it to the sum
sum += primevector[i]
if (sum > n):
break
if (isprm(sum) == 1):
count += 1
# Return the final count
return count
# Driver Code
# Given number N
N = 45
# Function call
print(countprime(N))
# This code is contributed by code_hunt
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if a
// number is prime or not
static int isprm(int n)
{
// Base Case
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 ||
n % 3 == 0)
return 0;
// Iterate till [5, Math.Sqrt(N)]
// to detect primality of numbers
for (int i = 5; i * i <= n;
i = i + 6)
{
// If N is divisible by i
// or i + 2
if (n % i == 0 ||
n % (i + 2) == 0)
return 0;
}
// Return 1 if N is prime
return 1;
}
// Function to count the prime numbers
// which can be expressed as sum of
// consecutive prime numbers
static int countprime(int n)
{
// Initialize count as 0
int count = 0;
// Stores prime numbers
List<int> primevector = new List<int>();
for (int i = 2; i <= n; i++)
{
// If i is prime
if (isprm(i) == 1)
{
primevector.Add(i);
}
}
// Initialize the sum
int sum = primevector[0];
// Find all required primes upto N
for (int i = 1; i < primevector.Count; i++)
{
// Add it to the sum
sum += primevector[i];
if (sum > n)
break;
if (isprm(sum) == 1)
{
count++;
}
}
// Return the readonly count
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given number N
int N = 45;
// Function Call
Console.Write(countprime(N));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// Javascript program for the above approach
// Function to check if a
// number is prime or not
function isprm(n)
{
// Base Case
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 || n % 3 == 0)
return 0;
// Iterate till [5, sqrt(N)] to
// detect primality of numbers
for (var i = 5;
i * i <= n; i = i + 6) {
// If N is divisible by i
// or i + 2
if (n % i == 0 || n % (i + 2) == 0)
return 0;
}
// Return 1 if N is prime
return 1;
}
// Function to count the prime numbers
// which can be expressed as sum of
// consecutive prime numbers
function countprime(n)
{
// Initialize count as 0
var count = 0;
// Stores prime numbers
var primevector = [];
for (var i = 2; i <= n; i++) {
// If i is prime
if (isprm(i) == 1) {
primevector.push(i);
}
}
// Initialize the sum
var sum = primevector[0];
// Find all required primes upto N
for (var i = 1;
i < primevector.length; i++) {
// Add it to the sum
sum += primevector[i];
if (sum > n)
break;
if (isprm(sum) == 1) {
count++;
}
}
// Return the final count
return count;
}
// Driver Code
// Given number N
var N = 45;
// Function Call
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))
Similar Reads
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 an
9 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
Count numbers up to N that can be expressed as powers of Prime Numbers Given an integer N, the task is to count numbers from the range [1, N] which are the power of prime numbers. Examples: Input: N = 6Output: 3Explanation:Numbers from the range [1, 6] that can be expressed as powers of prime numbers are:2 = 213 = 314 = 225 = 51 Input: N = 9Output: 7Explanation:Numbers
9 min read
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers Given a positive integer N, the task is to find the count of integers from the range [1, N] such that the integer cannot be expressed as sum of two or more consecutive positive integers. Examples: Input: N = 10Output: 4Explanation: The integers that cannot be expressed as sum of two or more consecut
8 min read
Find the prime numbers which can written as sum of most consecutive primes Given an array of limits. For every limit, find the prime number which can be written as the sum of the most consecutive primes smaller than or equal to limit.The maximum possible value of a limit is 10^4. Example: Input : arr[] = {10, 30} Output : 5, 17 Explanation : There are two limit values 10 a
14 min read
Express a number as sum of consecutive numbers Given a number N, write a function to express N as sum of two or more consecutive positive numbers. If there is no solution, output -1. If there are multiple solution, then print one of them.Examples: Input : N = 10 Output : 4 + 3 + 2 + 1 Input : N = 8 Output : -1 Input : N = 24 Output : 9 + 8 + 7 R
5 min read