prime sieve Prime number

index > Algebra > prime sieve


main content

This article only includes the faster linear sieve method, whose function is to find all prime numbers from 2 to n, and get the judgment array by the way.

Simply find prime numbers and mark all composite numbers. (Commonly used)

const int MAXN = -1;//10000005
int prime[MAXN],pnum;
bool is_composite[MAXN];

void sieve(const int &n) {
    
    
  	// 1 is exception
    //clock_t begin = clock();
    for (int i = 2; i < n; ++i) {
    
    
        if (!is_composite[i]) prime[++pnum]=i;
        for (int j = 1; j <=pnum  && i * prime[j] < n; ++j) {
    
    
            is_composite[i * prime[j]] = true;
            if (i % prime[j] == 0) break;
        }
    }
    //cout << (double) (clock() - begin) / CLOCKS_PER_SEC << endl;
}

While finding prime numbers, find the prime factor with the smallest composite number.

const int MAXN = -1;//10000005
int prime[MAXN], pnum;
int min_composite[MAXN];

void sieve(const int &n) {
    
    
  	// 1 is exception
    //clock_t begin = clock();
    for (int i = 2; i < n; ++i) {
    
    
        if (!min_composite[i]) {
    
    

            prime[++pnum] = i;
            min_composite[i] = i;
        }
        for (int j = 1; j <= pnum && prime[j] <= min_composite[i] && i * prime[j] < n; ++j) {
    
    
            min_composite[i * prime[j]] = prime[j];
//            if (i % prime[j] == 0) break;
        }
    }
    //cout << (double) (clock() - begin) / CLOCKS_PER_SEC << endl;
}

&& prime[j] <= min_composite[i]It's if (i % prime[j] == 0) break;almost the same, just choose one of them.

understanding

The idea of ​​Sieve of Eratosthenes is well understood. Every time a prime number is loaded, all multiples of this number are crossed out. But it is easy to find that many numbers will be crossed out multiple times. The time complexity is O (nloglogn) O(n log log n)O ( n l o g l o g n ) is not ideal.

Euler's sieve's perspective is actually different (although the codes of the two are very similar). In order to understand, it is best to look at the second code first. The following is my personal understanding.

Since composite numbers are not expected to be deleted repeatedly, we should try to make all composite numbers be removed by the smallest prime factor. This constitutes a transfer. Suppose pi pithe p- I Shiiithe smallest prime factor of i , then for allpiless than or equal topiprime number of p i pj pjp j有 ,pj ∗ i pj * ipji is a composite number, andpj pjp j is the smallest prime factor.

When enumerated to iiAt the time of i , there is no more thaniiAll the smallest prime numbers of i , then we can traversei ∗ prime [j] i * prime_{[j]}iprime[j] Delete the following numbers accordingly.

The deleted state is from the smallest prime number set prime [j] prime_{[j]}prime[j]So it can be understood that the new composite number is deleted by the smallest prime factor.

But it cannot be deleted all the time. When $ i% prime[j] == 0$ is established, it means that prime [j] prime[j] at this timep r i m e [ j ] happens to bethe smallest primeof $ i, the smallest prime of,The most small prime numbers , I * Prime _ {[+ J. 1]}multiple later, should be a multiple of the later, should theThe rear of times the number , should this by the deleted when I $ as the smallest prime factor.

$ prime[j] <= min_composite[i]$ is a similar judgment.

Although I don't know how to verify, the fact is that all numbers can be traversed by a constant number of times. In general, we don't need the smallest prime factor, so it can be optimized to boolean traversal.

lace

The earliest origin of this algorithm is related to Euler, but it does not seem to be proposed by Euler himself.

Euler’s proof of the zeta product formula contains a version of the sieve of Eratosthenes in which each composite number is eliminated exactly once.[8] The same sieve was rediscovered and observed to take linear time by Gries & Misra (1978).[19] It, too, starts with a list of numbers from 2 to n in order.

Later generations observed this algorithm and published a CACM journal paper, and the giant Euler just used this method to assist with a more important proof (Riemann), not taking it seriously...ORZ

But the concept of linear sieve can be extended to the method of finding integrative functions. To understand this algorithm has longer-term significance.

reference

Math note — linear sieve By Nisiyama_Suzune This blog talks about linear sieve

Sieve of Eratosthenes

Guess you like

Origin blog.csdn.net/Tighway/article/details/97790452