python sieve_of_eratosthenes.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python sieve_of_eratosthenes.py相关的知识,希望对你有一定的参考价值。

def sieve(n):
  candidates = set(range(2, n + 1))
  iterable = (number for number in xrange(2, int(math.sqrt(n)) + 1) if number in candidates)
        
  for prime in iterable:
    divisible = prime * 2
    while divisible <= n:
      if divisible in candidates: candidates.remove(divisible)
      divisible += prime
      
  for number in xrange(2, n + 1):
      if number in candidates: yield number

以上是关于python sieve_of_eratosthenes.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识