Linear sieve (Euler sieve) C language

Preface
Linear sieve is an efficient algorithm for finding all prime numbers less than or equal to a given value. It is an improved version of the Sieve of Eratosthenes that can calculate a large number of prime numbers in a shorter time. It has the advantages of low time complexity, low space complexity and strong scalability. Today we will explain to you the implementation of linear screen. Without further ado, let’s get started now!
Insert image description here

Article directory

principle

Any natural number except 1 can be divided by a prime number. This is because if it does not contain factors other than 1 and itself, then it is a prime number and is divisible by itself. Otherwise, its factors other than 1 and itself are divided. By the same discussion, it can be proved that it contains prime factors. That is to say, if we want to determine whether a number is prime, we need to find its smallest prime factor. If the smallest prime factor is equal to itself, then it is a prime number. Otherwise it is not a prime number.
Take the prime number 2 as an example. Except for 2, all its multiples are not prime numbers.

accomplish

First we need to create an array minp to store the minimum prime factor of each number, and then create an array prime to store the prime numbers. We need to screen out prime numbers from 2 to n (specified number), and at the same time find the minimum prime factors of each number. We just learned that the minimum prime factors of multiples of 2 are all 2, and the minimum prime factors of multiples of 3 are also It is also 3, which means that we can change all the values ​​of minp [multiples of prime numbers] to the value of the prime number through a loop. At this time, we may find some problems, such as 6 being a multiple of 2 and a multiple of 3. Then after traversing, wouldn't its minimum prime factor become 3 instead of 2? That's right, so we need to have a sentence like this to judge if the prime number currently traversed is equal to 0, we will stop the loop, or when the minimum prime factor of this number is equal to the prime number currently traversed, we will stop the loop, so It can solve the problem just now (it is best to read this sentence while looking at the code, otherwise it will be difficult to understand). So let’s get into the code!

#include<stdio.h>
int main()
{
    
    
	int prime[100] = {
    
     0 };//质数
	int minp[101] = {
    
     0 };//最小质因子
	int cnt = 0;
	for (int i = 2; i <= 100; ++i)
	{
    
    
		if (minp[i] == 0)
	   {
    
    
			minp[i] = i;
			prime[cnt++] = i;
       }
		for (int j = 0; j < cnt; ++j)
		{
    
    
			int p = prime[j];
			if (i * p > 100)
				break;
			minp[i * p] = p;
			if (minp[i] == p)
				break;
			//if(i%p==0) //用这个也可以
			//break;
		}
	}
	for (int i = 0; i < cnt; i++)
		printf("%d ", prime[i]);
	return 0;
}

end

Today’s linear sieve algorithm is introduced here. If you think the blogger’s talk is good, please give the blogger a follow, like, and favorite. The blogger will continue to share knowledge, see you in the next issue!

Guess you like

Origin blog.csdn.net/Tokai___Teio/article/details/134992635