Could you please take a look at my implementation of the sieve of eratosthenes in Python and tell me how can I improve/optimize it?
I'm a beginner in programming, so I don't have any ideas how to optimize it, and I'd really appreciate it if you check it out and tell me what can be improved.
# -*- 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