您能否看看我在 Python 中实现的 eratosthenes 筛,并告诉我如何改进/优化它?
我是编程的初学者,所以我不知道如何优化它,如果您检查一下并告诉我可以改进的地方,我将非常感激。
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 19:57:14 2013
@author: stefan
"""
def sqrt_int(n):
n = n**0.5
if n == int(n):
return True
else:
return False
def cbrt_int(n):
n = n**(1.0/3)
if n == int(n):
return True
else:
return False
def sieve(limit):
first_primes = [2,3,5,7]
primes = [x for x in range (2,limit+1)]
for y in first_primes:
primes = filter(lambda x: x % y != 0, primes)
primes = filter(lambda x: not sqrt_int(x), primes)
primes = filter(lambda x: not cbrt_int(x), primes)
if limit > 10:
primes = first_primes + primes
else:
primes = filter(lambda x: x <= limit, first_primes)
return primes