UVA 1210 Sum of Consecutive Prime Numbers Solution
Solution in c++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
#define M 100050
using namespace std;
bool marked[M];
vector<long long >vec;
void sieve( )
{
for(int i=4; i<=M; i+=2)
marked[i]=true;
for(int i=3; i*i<=M; i+=2)
{
if(marked[i]==false)
{
for(int j=i*i; j<=M; j+=i)
{
marked[j]=true;
}
}
}
vec.push_back(2);
for(int i=3; i<=M; i+=2)
{
if(marked[i]==false)
{
vec.push_back(i);
}
}
}
main()
{
int m,r,f,i,s,j,c;
sieve( );
while(scanf("%d",&m)!=EOF)
{
if(m==0)
break;
else
{
c=0;
for(i=0; vec[i]<=m; i++)
{
s=vec[i];
for(j=i+1; s<m; j++)
{
s=s+vec[j];
}
if(s==m)
c++;
}
cout<<c<<endl;
}
}
}
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
#define M 100050
using namespace std;
bool marked[M];
vector<long long >vec;
void sieve( )
{
for(int i=4; i<=M; i+=2)
marked[i]=true;
for(int i=3; i*i<=M; i+=2)
{
if(marked[i]==false)
{
for(int j=i*i; j<=M; j+=i)
{
marked[j]=true;
}
}
}
vec.push_back(2);
for(int i=3; i<=M; i+=2)
{
if(marked[i]==false)
{
vec.push_back(i);
}
}
}
main()
{
int m,r,f,i,s,j,c;
sieve( );
while(scanf("%d",&m)!=EOF)
{
if(m==0)
break;
else
{
c=0;
for(i=0; vec[i]<=m; i++)
{
s=vec[i];
for(j=i+1; s<m; j++)
{
s=s+vec[j];
}
if(s==m)
c++;
}
cout<<c<<endl;
}
}
}
No comments