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.
Input: N = 27
13 = 5 + 7 + 1 and 19 = 7 + 11 + 1 are the required prime numbers.
Input: N = 34
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:
- Check if an integer can be expressed as a sum of two semi-primes
- Length of largest sub-array having primes strictly greater than non-primes
- Find the prime numbers which can written as sum of most consecutive primes
- Count Primes in Ranges
- Count number of primes in an array
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count numbers which can be represented as sum of same parity primes
- Circular primes less than n
- Palindromic Primes
- Print all safe primes below N
- Print all multiplicative primes <= N
- Alternate Primes till N
- Product of all primes in the range from L to R
- Print all Proth primes up to N
- Sum of all Primes in a given range using Sieve of Eratosthenes
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.
Improved By : prerna saini