Primes Problem
Problem Description
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
Input
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000) .
Output
For each test case, print the number of ways.
Sample Input
3
9
Sample Output
0
2
#include <bits/stdc++.h>///Primes Problem
using namespace std;
#define N 10005
int prime[N];
int q[N];
int main()
{
int i,j,k,n,l=0,sum;
memset(q,0,sizeof(q));
prime[0]=0;
prime[1]=0;
prime[2]=1;
for(i=3;i<N;i++)//首先偶数一定不是素数,除了2
{
if(i%2)
prime[i]=1;
else
prime[i]=0;
}
for(i=3;i<=sqrt(N);i++)
{
if(prime[i])
for(j=i+i;j<N;j=j+i)//当i是素数的时候,i的所有的倍数必然是合数
prime[j]=0;
}
for(k=0;k<10000;k++)
{
if(prime[k])
q[l++]=k;
}
while(~scanf("%d",&n))
{
sum=0;
for(int i=0;q[i]<n;i++)
for(int j=i;q[j]<n-q[i];j++)
{
if(prime[n-q[i]-q[j]]&&n-q[i]-q[j]>=q[j])
{
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}