Sieve method, sieve method

Source: Internet
Author: User

Sieve method, sieve method

2016.1.25

 

We all know that determining whether a number is a prime number can be solved within the time complexity of O (√ n. However, if the number of prime numbers in [1, n] is required, it is obviously a little slow to judge one by one.

But we know an obvious property: all the prime factors of a given number are smaller than this sum, and there is no prime factor smaller than a prime number.

Then, we can use the obtained prime factor to compare the total number of bins to perform sieve division.

The specific operation is as follows: first, we will write down all the numbers in 2 to n. 2 is the decimal number in the table, that is, the prime number, and then all the multiples of 2 are excluded. At this time, the remaining minimum number in the table is 3, that is, 3 is the prime number, and then the multiples of all three in the table are crossed. After this operation is performed, the smallest number in each table is a prime number and its multiples are all crossed.

 

 

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Multiple of sieve 2

2

3

-

5

-

7

-

9

-

11

-

13

-

15

-

17

-

19

-

Multiple of 3

2

3

-

5

-

7

-

-

-

11

-

13

-

-

-

17

-

19

-

The Code is as follows:

Int n, prime [1000005], e; bool is_prime [1000005]; // If is_prime is 0, void solve () // prime screening {prime [0] = prime [1] = 1; for (int I = 2; I <= n; I ++) {if (! Is_prime [I]) {prime [++ e] = I; if (long) I * I> n) continue; // prevent int for (int j = I * I; j <= n; j + = I) is_prime [j] = 1 ;}}}View Code

The time complexity of this screening method is not obvious. It looks like O (n), and the actual measurement is indeed close to linear under noip data scale, however, his time complexity is actually O (n loglogn), which seems to be a linear constant with a slight increase.

The author does not understand the time complexity of this strange story, but posted it here. If there is a better proof, he can tell me the story.

(Transfer)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.