An iterator that produces prime numbers

Source: Internet
Author: User
Tags bit set in python

This article describes two ways to write prime numbers iterators in Python.

The first method is very simple:

Def genprime (): Yield 2 for N in countodd (1): If IsPrime (n): yield n

The idea of this method is to determine whether it is a prime number in all odd numbers except 2. Countodd is an odd-numbered iterator. You can define this by:

def countodd (n): while True:yield n n+=2

The rest is how to write the IsPrime method. There is no shortcut to go, only to follow the definition of prime numbers honest to the poor. Of course we can use a little trick to get the complexity out of

O (n) drops to O (n^0.5).

def isprime (n): If N<2:return false for I in range (2,int (MATH.SQRT (n)) +1): If N%i==0:return false return True

Finally, its total time complexity is O (n^1.5).

The second method is based on the ancient screening method. The filtering method is to first list the natural number within a certain range, then delete the multiples of 2, multiples of 3, multiples of 5. The rest is prime.

Thanks again to Python's iterators so that we don't really have to list these natural numbers. Otherwise it's going to be a sore point. If you've seen this article using Python to solve Google treasure Hunt 2008 Question:primes, you must have seen this approach.

def sieve (): "" "Sieve of Eratosthenes, yield each prime in sequence." "" yield 2 D = {} q = 3 while true:p = D.pop (q, 0) I F p:x = q + p while x in D:x + = p d[x] = p Else:yield q d[q*q] = 2*q Q + 2

It would be a bit strange to see the screening method for the first time. Because we usually use a bit set when we write the filtering method. The dictionary is used here.

Multiples of prime numbers are temporarily inserted into the dictionary as key values. So the basis of judgment is that if a number is present in the dictionary, it must not be prime.

Because the dictionary inserts, deletes, the search complexity All is O (1), therefore this method efficiency is very good. The time complexity of each execution is O (1), and the total time complexity is O (n). But the total number of saved numbers in the dictionary increases with Q, and the value is approximately Q/LN (q). The growth rate is the blue line in the figure below. The red line is n^0.5 and the green is N.

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.