定理:
1. 任何大于5的奇数都可以写成3个素数之和。
2.任何大于2的偶数都可以写成两个素数之和。
Description
给定一个数
n
,求出
n
最少可以表示为多少个素数之和。
Input
第一行一个正整数
n(1<n<2000000000)
。
Output
一个整数表示答案
Sample Input
4
Sample Output
2
分析: 对于偶数,肯定是由两个素数组成(2除外)。
对于奇数:
1.如果本身就是素数,就是1.
2.如果n-2还是素数,则n可以写成(n-2)加2。所以结果是2. (为什么是减2,而不是减3: 因为n减2之后还是奇数,但n减3之后就变成了偶数)
3.其余情况就是3.
代码:
#include<iostream>
#include<cmath>
using namespace std;
bool judge(int t)
{
for(int i=2;i<sqrt(t)+1;i++){
if(t%i==0) return false;
}
return true;
}
int main()
{
int n;
while(cin >> n){
if(n==2) cout << 1 << endl;
else if(n&1){
if(judge(n)) cout << 1 << endl;
else if(judge(n-2)) cout << 2 << endl;
else cout << 3 << endl;
}
else cout << 2 << endl;
}
}