Prime Screening - bailian 4138: and the product of primes

Topic Link

http://bailian.openjudge.cn/practice/4138/

Primes Esieve screening


Screenshot from Baidu Encyclopedia
that this question can find from the position s / 2 of the forward and backward primes table, find the first pair of prime numbers is the greatest prime product.

cpp Code

#include <cstdio>
#include <cstring>
#include <cmath>
#define MAX 10010

int prime[MAX];

void pick(int n){
    memset(prime, -1, sizeof(prime));
    prime[1] = 0;
    
    for(int i = 2; i*i <= n; i++)
        if(prime[i])
            for(int j = i + i; j <= n; j+= i)
                prime[j] = 0;
}

int main(){
    int s;
    pick(MAX);
    
    scanf("%d", &s);
    if(s % 2){
        if(prime[s-2])
            printf("%d\n", 2 * (s - 2));
    }
    else{
        int i, j;
        for(i = j = s/2; !prime[i] || !prime[j]; i--, j++);
        printf("%d\n", i * j);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhangyue123/p/12611492.html