The Goldbach Conjecture states that any even number x ≥ 4 can be expressed as the sum of two primes. It can be verified that the conjecture is true for all x ≤ 106 .
Define a Goldbach step as taking x (4 ≤ x ≤ 106 ), finding primes p and q (with p ≤ q) that sum to x, and replacing x with q − p. If there are multiple pairs of primes which sum to x, we take the pair with the largest difference. That difference must be even and less than x. Therefore, we can repeat more Goldbach steps, until we can reach a number less than 4
Given x, find how many Goldbach steps it takes until reaching a number less than 4.
Input
The input will consist of a single integer x (4 ≤ x ≤ 106 ).
Output Print,
on a single line, the number of Goldbach steps it takes to reach a number less than 4.
Sample Input and Output
20 3
30 4
40 5
#include<bits/stdc++.h>
using namespace std;
int f(int a)
{
int i;
if(a==0||a==1) return 0;
int flag=0;
for(i=2;i<=sqrt(a);i++)
{
if(a%i==0) {flag=1;break;}
}
if(!flag) return 1;
return 0;
}
int main()
{
int n;
cin>>n;
int cnt=0;
int i=2;
while(n>=4)
{
for(;i<=n/2;i++)
{
if(f(i)) break;
}
int a;
a=n-i;
if(f(a))
{
n=a-i;i=2;cnt++;
}
else
{
i++;
}
}
printf("%d",cnt);
}
主要是对素数的判断,这里要注意,此题卡sqrt求素数,不用sqrt就会超时!