半素数 哥德巴赫猜想(任何大于六的偶数都可以写成两个奇质数的和)
题目定义了一种叫半素数的数:只要一个数能被分解成两个素数,那么这个数就是半素数。
Prime Number Definition
An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are prime numbers but 8, 20, 27 are not.
Semi-Prime Number Definition
An integer greater than one is called a semi-prime number if it can be decompounded to TWO prime numbers. For example, 6 is a semi-prime number but 12 is not.
Your task is just to determinate whether a given number is a semi-prime number.
Input
There are several test cases in the input. Each case contains a single integer N (2 <= N <= 1,000,000)
Output
One line with a single integer for each case. If the number is a semi-prime number, then output "Yes", otherwise "No".
Sample Input
3
4
6
12
Sample Output
No
Yes
Yes
No
没什么好说的,解法很简单,解法如下:
#include <iostream> #include <cmath> using namespace std; bool isprime(long test) { int i; for(i=2;i<=sqrt((long double)test);i++) { if(test%i ==0) return false; } return true; } bool isSemiPrime(long test) { int i; for(i=2;i<=sqrt((long double)test);i++) { if(test%i ==0) { int temp = test/i; return isprime(i) && isprime(temp); } } return false; } int main() { long n; while(cin>>n && n !=0) { if(isSemiPrime(n)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }