ZOJ2723 Semi-Prime【素数筛选+试探法】

Semi-Prime

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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



Author: LIU, Yaoting
Source: Zhejiang University Local Contest 2006, Preliminary


问题链接ZOJ2723 Semi-Prime

题意简述半素数定义为两个素数的乘积,输入若干个整数(2<=n<=1000000),判定其是否为半素数。

问题分析:用Eratosthenes筛选法设置一个素数判定数组,然后再用试探法就可以判定。

程序说明:(略)

参考链接:(略)


AC的C++语言程序如下:

/* ZOJ2723 Semi-Prime */

#include <iostream>
#include <string.h>
#include <math.h>

using namespace std;

const int N = 1000000 / 2;
bool prime[N+1];

// Eratosthenes筛选法
void sieveofe(int n)
{
    memset(prime, true, sizeof(prime));

    prime[0] = prime[1] = false;
    for(int i=2; i<=sqrt(n); i++) {
        if(prime[i]) {
            for(int j=i*i; j<=n; j+=i)  //筛选
                prime[j] = false;
        }
    }
}

int main()
{
    sieveofe(N);

    int n;
    while(cin >> n) {
        int end = sqrt(n);
        bool flag = false;
        for(int i=2; i<=end; i++) {
            if(prime[i]) {
                int j = n / i;
                if(prime[j] && i * j == n) {
                    flag = true;
                    break;
                }
            }
        }

        cout << (flag ? "Yes" : "No") << endl;
    }

    return 0;
}




原文地址:https://www.cnblogs.com/tigerisland/p/7563695.html