Return to Snippet

Revision: 6985
at June 29, 2008 12:58 by Kartoffel


Initial Code
# -*- 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]]

Initial URL


Initial Description
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.

Initial Title
Sieve of Eratostenes

Initial Tags


Initial Language
Python