[Algorithm] Prime sieves and linear sieves (Essier sieve, Euler sieve)

Remarks

2020/3/7 Saturday
Recently, I suddenly saw an article about finding prime numbers (also called prime numbers) at my home. It explains how to find prime numbers through Ehrlich sieve and Euler sieve. In many cases, finding prime numbers is very important. Good algorithms can greatly improve the operation of the program. effectiveness.

One, the conventional method

1. Principle and optimization

The most direct way to find prime numbers is to enumerate. To judge whether n is a prime number, you only need to try the numbers less than n in turn to see if n can be divisible.But in fact we only need to judge n \sqrt{n}n Can, Because if n is not a prime number, then the factor of n is divided by n \sqrt{n}n Outer must exist in pairs, and one is more than n \sqrt{n}n One bigger than n \sqrt{n}n Small, so find n \sqrt{n}n Can (But for convenience, i*i<=n is generally used to judge in the program). This method is often used to determine whether an individual number is a prime number.

2. Algorithm implementation

bool prime(int n){
    
    
	for(int i=2;i*i<=n;i++){
    
    
		if(n%i==0) return false;
	}
	return true;
}

3. Time complexity

We can easily see that this is the time complexity of the algorithm isO ( n \ sqrt {n}n )


Limitations:
When we need to get all prime numbers within the natural number n, this method is no longer easy to use.


Second, the sieve

1. Principle

The Eratosthenes sieve method, abbreviated as Ertosthenes or Ess sieve, is a simple algorithm for verifying prime numbers proposed by the Greek mathematician Eratosthenes. In order to get all prime numbers within the natural number n,All multiples of prime numbers that are not greater than the radical n must be eliminated, and the rest are prime numbers. Given the range n of the values ​​to be sifted, find the prime numbers within. First use 2 to sieve, that is, leave 2 and eliminate multiples of 2; then use the next prime number, which is 3 sieves, leave 3 and eliminate multiples of 3; then use the next prime number 5 sieves , Leave 5, and eliminate multiples of 5; keep repeating... In this process, we generally create a flag array to store whether a certain number has been eliminated.


Thinking:
In my opinion,The process of removing non-prime numbers is actually a process of creating composite numbers,due toThe basic theorem of arithmetic (every composite number can be written as a product of prime numbers in a unique form, that is, decomposing prime factors. ) We can "create" composite numbers by multiplying the known prime numbers by natural numbers (from 1 to n), and the numbers that are not created in this process are prime numbers. We repeat this process with new quality. Directly find all the qualities within n.

2. Algorithm implementation

bool num[MAX]={
    
    0};	//数字是否被剔除
int primenum[MAX];	//存放素数
void prime(int n){
    
    
	for(int i=2;i*i<=n;i++){
    
    
		if(!num[i]){
    
    			//如果数字i是素数
			for (int j=i*i; j<=n; j+=i){
    
    	//剔除i的倍数(这里从i*i开始比从2*i开始节省时间,因为更小的数字已经被剔除过一次了)
				num[j*i]=true;	
			}
		}
	}
	for(int i=2;i<=n;i++){
    
    			//现在num数组值为0的数字就是素数,我们把他们存到primenum中
		if(!num[i]) primenum[t++]=i;
	}
}

3. Time complexity

The time complexity of the sieve isO (nloglogn), The derivation process can search by itself.


Limitations:
We made multiple judgments for a non-prime number, which caused waste.


Three, Euler Sieve

1. Principle

In order to solve the problem of multiple judgments wasting time, Euler Sieve was born. On the basis of the Ehrlich sieve method,Let each composite number be screened only once by its smallest prime factor, In order to achieve the goal of non-repetition. To this end, we need an array to store prime numbers, and then
multiply each natural number by a known prime number in turn, and make some judgments in this process to achieve the purpose of screening with the smallest prime number.


Thinking:
Same as the Ehrlich Sieve, Euler Sieve is also a process of creating composite numbers, but for each composite number, we only create it once, by multiplying his smallest prime factor by his largest factor (except ourselves). Achieved in this way. For this largest factor, it may be a prime number or a composite number. When it is a prime number, due to the fundamental theorem of arithmetic (Each composite number can be written as a product of prime numbers in a unique form, that is, decomposing prime factors. ), then the composite number we construct must be new (the first time it is constructed by our program); when it is a composite number, due to the basic theorem of arithmetic, we can regard this composite number as the quality that constitutes it. The product of factors, in order to ensure that the composite number constructed this time is a new one, I only need to calculate the remainder of the composite number in ascending order of the known prime numbers. When the composite number is divisible by a certain prime number, it is less than or equal to this prime number. The prime numbers of can be combined with this composite number to construct a new composite number, but the prime numbers greater than this composite number cannot. At this point we can start the next round of construction. Because when the prime number we use is larger than the prime number obtained by decomposition of the known composite number, we have already constructed the composite number with a smaller prime number.

2. Algorithm implementation

int t=0;			//已求出的质数的个数
int primenum[MAX];	//质数列
bool num[MAX]={
    
    0};	//判断是否被剔除
void prime(int n){
    
    
	for(int i=2;i<=n;i++){
    
    
		if(!num[i]){
    
    	//如果i没有被剔除,就加入质数列
			primenum[t++]=i;
		}
		for(int j=0;j<t;j++){
    
    	//构造合数
			if(i*primenum[j]>n) break;	//构造的合数大于需要的范围n
			num[i*primenum[j]]=true;	//剔除构造的合数
			if(i%primenum[j]==0) break;	//数字i可以分解出与当前素数相同的素数,我们就不能继续使用更大的素数了
		}
	}
}

3. Time complexity

Euler sieve is aO (n) The first-level algorithm is really genius-like wisdom.


Welcome to put forward your valuable opinions in the comment area

Guess you like

Origin blog.csdn.net/l1447320229/article/details/104717087