1259. Sum of consecutive Primes
Description
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has
Three Representations 2 + 3 + 5 + 7 + 11 + 13, 11 + 13 + 17, and 41. the integer 3 has only one representation, which is 3. the integer 20 has no such representations. note that summands must be consecutive prime
Numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Input
The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.
Output
The output shoshould be composed of lines each corresponding to an input line should t the last zero. an output line des the number of representations for the input Integer as the sum of one or more consecutive prime numbers. no other characters shoshould be inserted
In the output.
Sample Input
2317412066612530
Sample output
11230012
Calculate the continuous prime number and the number of cases for a certain number. The prime number is obtained by using the limit method, and two-layer loops are implemented.
#include <iostream>#include <map>#include <cstring>using namespace std;map<int,int>m;bool Prime[10007];int P[10005],n;const int maxn=10000;int main(){n=0;memset(Prime,1,sizeof(Prime));Prime[0]=Prime[1]=0;for(int i=2;i<=maxn;++i) {if(Prime[i]){P[n++]=i;for(int j=2;j*i<=maxn;++j){Prime[j*i]=0;}}}for(int i=0;P[i]!=0;i++)m[P[i]]=i;int n;while(cin>>n&&n){int count=0;if(Prime[n]) count++;for(int i=2;i<=n;i++){if(Prime[i]==0)continue;int sum=i;int j=P[m[i]+1];while(Prime[j]!=0&&sum<=n){sum+=j;if(sum==n){count++;break;}j=P[m[j]+1];}}cout<<count<<endl;}return 0;}
1259. Sum of consecutive Primes
Description
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has
Three Representations 2 + 3 + 5 + 7 + 11 + 13, 11 + 13 + 17, and 41. the integer 3 has only one representation, which is 3. the integer 20 has no such representations. note that summands must be consecutive prime
Numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Input
The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.
Output
The output shoshould be composed of lines each corresponding to an input line should t the last zero. an output line des the number of representations for the input Integer as the sum of one or more consecutive prime numbers. no other characters shoshould be inserted
In the output.
Sample Input
2317412066612530
Sample output
11230012