/ Published in: Python
                    
                                        
An (probably inefficient) implementation of the Sieve of Eratostenes. The function returns a list containing all the prime numbers between 0 and the number passed to it.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
# -*- coding: utf-8 -*-
def eratostenes(num, print_nums=False):
if num < 3:
raise Exception, 'Number is too small'
full_nums = range(num)[1:] + [num]
primes = [False] + [True]*(num - 1)
for i in range(num):
if primes[i]:
if print_nums:
print full_nums[i]
inc = full_nums[i]
cursor = i + inc
if cursor > num:
break
while cursor < num:
primes[cursor] = False
cursor += inc
return [full_nums[j] for j in range(num) if primes[j]]
Comments
 Subscribe to comments
                    Subscribe to comments
                
                