Sieve of Eratosthenes

From Rosetta Code
Revision as of 21:37, 18 October 2007 by rosettacode>Drea (→‎Sieve of Eratosthenes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Sieve of Eratosthenes
You are encouraged to solve this task according to the task description, using any language you may know.

The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

MAXScript

fn eratosthenes n =
(
    multiples = #()
    print 2
    for i in 3 to n by 2 do
    (
        if (findItem multiples i) == 0 then
        (
            print i
            for j in (i + i) to n by i do
            (
                append multiples j
            )
        )
    )
)

eratosthenes 100

Python

def eratosthenes(n):
    multiples = []
    print 2
    for i in xrange(3, n, 2):
        if not i in multiples:
            print i
            for j in xrange((i+i), n, i):
                multiples.append(j)

eratosthenes(100)