Sieve for prime numbers, Espersky sieve and Euler sieve (linear sieve)

Find all prime numbers within len

In addition to the more commonly used method of finding the root number, there are two better methods, the Escher sieve and the linear sieve. Among them, the Escher sieve is better understood, while the linear sieve (Euler sieve) is not easy to understand but is faster.

Essiere sieve

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int len = 100;
    vector<int> primes;

    bool isPrime[len+2];
    memset(isPrime, 1, len);

    for (int i = 2; i <= len; ++i)
    {
        if (isPrime[i])
        {
            primes.push_back(i);
            for (int j = i*i; j <= len; j += i)
                isPrime[j] = false;
        }
    }

    for (vector<int>::iterator iter = primes.begin(); iter != primes.end(); ++iter)
        cout<<*iter<<" ";
    cout<<endl;
}

The result is:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

explain:

  • vector<int> primesIt is used to place prime numbers, and isPrimeis used to judge whether they are prime numbers. Initially, all are true, that is, all are prime numbers. Then put it 2和3in primesthe setting.
  • Then ifrom 2the beginning, traverse to len, if iit is a prime number, put it primesin, and if it iis a prime number, then all ithe multiples must not be prime numbers. So 2*i、3*i、4*i……neither is a prime number. So we can continue to isPrimeset these numbers to false.
  • But our loop starts from i*ithe beginning, because 2*i、3*i到(i-1)*iall composite numbers of , must have been falseset . Intuitively, it is indeed true. For example, 3*3 is 9, 8 will be judged as a composite number when 4*2, 5*5 is 25, and 24 will be judged as a composite number when 8*3.
  • The specific proof is not, you can see this .

Euler Sieve (Linear Sieve)

The Esperanto sieve will repeat the situation, for example, 40, when it is judged to 2, 20 2 is 40, and it is judged that 40 is a composite number, and when it is judged to 5, 8 5=40, it is also judged that 40 is a composite number, so 20 was judged twice. This is the Euler sieve.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int len = 100;
    vector<int> primes;

    bool isPrime[len+2];
    memset(isPrime, 1, len);

    for (int i = 2; i <= len; ++i)
    {
        if (isPrime[i] == true)
            primes.push_back(i);
        for (int j = 0; j < primes.size(); ++j)
        {
            if (i * primes[j] > len)
                break;
            isPrime[i * primes[j]] = false;
            if (i % primes[j] == 0)
                break;
        }
    }

    for (vector<int>::iterator iter = primes.begin(); iter != primes.end(); ++iter)
        cout<<*iter<<" ";
    cout<<endl;
}
  • vector<int> primesIt is used to place prime numbers, and isPrimeis used to judge whether they are prime numbers. Initially, all are true, that is, all are prime numbers. Then put it 2和3in primesthe setting.
  • Then ifrom 2the beginning, traverse to len, if iit is a prime number, put it primesin. At the same time, we primes[j]start traversing all the prime numbers we have judged now, then i*primes[j]they must not be prime numbers, so set it isPrimeto false.
  • But primes[j]when traversing, if i % primes[j] == 0, then there is no need to look at the next one i*primes[j+1]、i*primes[j+2]……, directly break. This is because if i / primes[j] = m, then i = m * primes[j], then i*primes[j+1] = m * primes[j] * primes[j+1] = (m*primes[j+1])*primes[j] = n*primes[j], that is to say, the number will i*primes[j+1]be judged at and will n*primes[j]be judged at, and nit must be greater ithan, because i*primes[j+1] = n*primes[j], but primes[j+1] > primes[j]. Then we wait until when ithe traversal arrives n, and then i*primes[j+1]judge the number as a composite number.
  • We can print out the composite numbers that are screened out each time:
i is 3, primes[j] is 2, multi is 6
i is 3, primes[j] is 3, multi is 9
i is 4, primes[j] is 2, multi is 8
i is 5, primes[j] is 2, multi is 10
i is 5, primes[j] is 3, multi is 15
i is 5, primes[j] is 5, multi is 25
i is 6, primes[j] is 2, multi is 12
i is 7, primes[j] is 2, multi is 14
i is 7, primes[j] is 3, multi is 21
i is 7, primes[j] is 5, multi is 35
i is 7, primes[j] is 7, multi is 49
i is 8, primes[j] is 2, multi is 16
i is 9, primes[j] is 2, multi is 18
i is 9, primes[j] is 3, multi is 27
i is 10, primes[j] is 2, multi is 20
i is 11, primes[j] is 2, multi is 22
i is 11, primes[j] is 3, multi is 33
i is 11, primes[j] is 5, multi is 55
i is 11, primes[j] is 7, multi is 77
i is 12, primes[j] is 2, multi is 24
i is 13, primes[j] is 2, multi is 26
i is 13, primes[j] is 3, multi is 39
i is 13, primes[j] is 5, multi is 65
i is 13, primes[j] is 7, multi is 91
i is 14, primes[j] is 2, multi is 28
i is 15, primes[j] is 2, multi is 30
i is 15, primes[j] is 3, multi is 45
i is 16, primes[j] is 2, multi is 32
i is 17, primes[j] is 2, multi is 34
i is 17, primes[j] is 3, multi is 51
i is 17, primes[j] is 5, multi is 85
i is 18, primes[j] is 2, multi is 36
i is 19, primes[j] is 2, multi is 38
i is 19, primes[j] is 3, multi is 57
i is 19, primes[j] is 5, multi is 95
i is 20, primes[j] is 2, multi is 40
i is 21, primes[j] is 2, multi is 42
i is 21, primes[j] is 3, multi is 63
i is 22, primes[j] is 2, multi is 44
i is 23, primes[j] is 2, multi is 46
i is 23, primes[j] is 3, multi is 69
i is 24, primes[j] is 2, multi is 48
i is 25, primes[j] is 2, multi is 50
i is 25, primes[j] is 3, multi is 75
i is 26, primes[j] is 2, multi is 52
i is 27, primes[j] is 2, multi is 54
i is 27, primes[j] is 3, multi is 81
i is 28, primes[j] is 2, multi is 56
i is 29, primes[j] is 2, multi is 58
i is 29, primes[j] is 3, multi is 87
i is 30, primes[j] is 2, multi is 60
i is 31, primes[j] is 2, multi is 62
i is 31, primes[j] is 3, multi is 93
i is 32, primes[j] is 2, multi is 64
i is 33, primes[j] is 2, multi is 66
i is 33, primes[j] is 3, multi is 99
i is 34, primes[j] is 2, multi is 68
i is 35, primes[j] is 2, multi is 70
i is 36, primes[j] is 2, multi is 72
i is 37, primes[j] is 2, multi is 74
i is 38, primes[j] is 2, multi is 76
i is 39, primes[j] is 2, multi is 78
i is 40, primes[j] is 2, multi is 80
i is 41, primes[j] is 2, multi is 82
i is 42, primes[j] is 2, multi is 84
i is 43, primes[j] is 2, multi is 86
i is 44, primes[j] is 2, multi is 88
i is 45, primes[j] is 2, multi is 90
i is 46, primes[j] is 2, multi is 92
i is 47, primes[j] is 2, multi is 94
i is 48, primes[j] is 2, multi is 96
i is 49, primes[j] is 2, multi is 98
i is 50, primes[j] is 2, multi is 100

Each composite number is screened only once

Summarize

  • Both methods first assume that all numbers are prime numbers, and then sift out composite numbers from them.
  • The Esperanto sieve is to screen out all the multiples of the prime number every time a prime number is obtained, because they must be composite numbers.
  • The Euler sieve traverses all the currently determined prime numbers every time it is judged whether it is a prime number, iand sieves them out.2到nki*k
  • But the Euler sieve will, when necessary , ensure that the sum and prime number breakthat are screened out each time are the smallest prime numbers . For example, 20 can be equal to , or it can be , here are respectively , and the prime numbers are respectively , we will screen out 20 here, but here, because it is the smallest prime number .i*kk4*510*2i4和10k5和210*24*52

Guess you like

Origin blog.csdn.net/qq_43219379/article/details/123755779