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.
Input: N = 45
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
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 in the counter.
Below is the implementation of the above approach:
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))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count primes that can be expressed as sum of two consecutive primes and 1
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number can be expressed as a sum of consecutive numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Elements of Array which can be expressed as power of prime numbers
- Count of primes below N which can be expressed as the sum of two primes
- Count of primes in a given range that can be expressed as sum of perfect squares
- Number expressed as sum of five consecutive integers
- Check if a number can be expressed as a product of exactly K prime divisors
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number can be expressed as sum two abundant numbers
- Numbers within a range that can be expressed as power of two numbers
- N expressed as sum of 4 prime numbers
- Check if an integer can be expressed as a sum of two semi-primes
- Check if a number can be expressed as sum of two Perfect powers
- Check whether a number can be expressed as a product of single digit numbers
- Check if N can be expressed as product of 3 distinct numbers
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as power | Set 2 (Using Log)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.