关于【素数筛法】

首先考虑一个命题:若一个数不是素数,则必存在一个小于它的素数为其的因数。这个命题的正确性是显而易见的。

那么,假如我已经获得了小于一个数的所有素数,我们只需要确定该数不能被这些素数整除,这个数即为素数。

换个角度:在我获得了一个素数时,将它的所有倍数标记为非素数,这样当我们遍历到一个数时,它没有被任何小于它的素数标记为非素数,确定

其为素数。

 

素数

题目描述:

输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。

输入:

输入有多组数据。
每组一行,输入n。

输出:

输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数(素数之间用空格隔开,最后一个素数后面没有空格),如果没有则输出-1。

样例输入:
100
样例输出:
11 31 41 61 71


Code:
#include <cstdio>
 
using namespace std;
 
void initPrime(bool mark[],int n){
    for(int i=1;i<=n;++i){
        mark[i]=true;
    }
    for(int j=2;j<=n;++j){
        if(mark[j]==false)
            continue;
        for(int k=2*j;k<=n;k=k+j){
            mark[k]=false;
        }
    }
}
 
int main()
{
    int n;
    const int arrSize=100000;
    bool mark[arrSize];
    while(scanf("%d",&n)!=EOF){
        initPrime(mark,n);
        bool havePrime=false;
        bool isFirst=true;
        for(int i=2;i<n;++i){
            if(mark[i]==true){
                if(i%10==1){
                    if(isFirst){
                            printf("%d",i);
                            havePrime=true;
                            isFirst=false;
                    }else{
                        printf(" %d",i);
                    }
                }
            }
        }
        if(havePrime==false)
            printf("-1");
        printf("
");
    }
    return 0;
}
 
/**************************************************************
    Problem: 1163
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1044 kb
****************************************************************/

Goldbach's Conjecture

题目描述:

Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. 
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

输入:

An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

输出:

Each output line should contain an integer number. No other characters should appear in the output.

样例输入:
6
10
12
0
样例输出:
1
2
1


Code:
#include <cstdio>
 
using namespace std;
 
const int arrSize=1000000;
bool prime[arrSize];
 
void initPrime(int num){
    for(int i=2;i<=num;++i){
        prime[i]=false;
    }
    for(int i=2;i<=num;++i){
        if(prime[i]==true)
            continue;
        for(int j=2*i;j<=num;j=j+i){
            prime[j]=true;
        }
    }
}
 
int main()
{
    int num;
    while(scanf("%d",&num)!=EOF){
        if(num==0)
            break;
        initPrime(num);
        int cnt=0;
        for(int i=2;i<=num/2;++i){
            if(prime[i]==false&&prime[num-i]==false)
                ++cnt;
        }
        printf("%d
",cnt);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1440
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1996 kb
****************************************************************/


原文地址:https://www.cnblogs.com/Murcielago/p/4197137.html