Ernie Eratosthenes Sieve method and common method for prime list (Python implementation)

Source: Internet
Author: User
Tags for in range square root

The method of screening for the Colorado:

To get a number that is not greater than N, you can first find all primes that do not exceed the square root n , set 2 = P1 < P2 < ... <pk≤√n, and then do the following in 2,3,4......N:

Leave P1 = 2, cross out the multiples of P1,

Leave the P2 again, and cross out the multiples of P2,

Continue this process, until the PK, the PK multiples all crossed off,

Finally, the total number of primes is not more than N.

Example:

N = 30, the PK is 5, so all primes 2 to 5 are 2,3,5

First time leave 2, row all multiples of 2

2 3 4 5 6 7 8 9

All in one .

All in all

The second time left 3, row all multiples of 3

2 3 4 5 6 7 8 9

all in one.

All in all

The third time left 5, row all multiples of 5

2 3 4 5 6 7 8 9

All in one .

All in all

The remaining number is all primes that are less than or equal to 30, i.e. 2,3,5,7,11,13,17,19,23,29

Algorithm implementation:

The algorithm idea comes from the above introduction, but not strictly follow the above steps:

def Eladuosai (n):     = List (range (1,n+1))    = 0    for in range (2,n+1):         if l[i-1]! = 0            :for in range (i*2,n+1, i):                l[j-1] =  0      for inch if x! = 0    ]return result

Common algorithm for all primes less than or equal to N:

def Sushu (n):     = []    for in range (2,n+1):        for in range (2  , x):             if x% y = = 0                :break        Else:            result.append (x)     return result    

Time comparison, using the Timeit module to test the time of two methods, when the n is 10000, there are the following conclusions:

T1 = Timeit. Timer ('Sushu (10000)', setup='From __main__ import Sushu') T2= Timeit. Timer ('Eladuosai (10000)', setup='From __main__ import Eladuosai')    Print('the time of the Eratosthenes screening method', T2.timeit (1))    Print('time of the normal function:', T1.timeit (1))

Ernie Eratosthenes time of screening 0.005523548190824634
Normal method Time: 0.7220688150193577

It can be seen that the operation time of the Eratosthenes sieve is much less than that of the normal method.

Ernie Eratosthenes Sieve method and common method for prime list (Python implementation)

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.