-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathConsecutivePrimeSum.cpp
53 lines (43 loc) · 1.21 KB
/
ConsecutivePrimeSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Consecutive Prime Sum
// Some prime numbers can be expressed as Sum of other consecutive prime numbers.
// For example
// 5 = 2 + 3
// 17 = 2 + 3 + 5 + 7
// 41 = 2 + 3 + 5 + 7 + 11 + 13
// Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
// Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.
// Input Format:
// Each test case contains a number N <= 1000000000
// Output Format:
// Print the total number of all such prime numbers which are less than or equal to N.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int isPrime(int n)
{
for(int i=2;i<=(int)sqrt(n);i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i,j,n,ct=0,sum=2;
cin>>n;
for(i=3;sum<n;i+=2)
{
if(isPrime(i))
{
sum+=i;
if(isPrime(sum) && sum<=n)
ct++;
}
}
cout<<ct;
return 0;
}