01.04.2015 Views

Applications: Sieve of Eratosthenes, Recursion - seanho

Applications: Sieve of Eratosthenes, Recursion - seanho

Applications: Sieve of Eratosthenes, Recursion - seanho

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Prime sieve: Python code<br />

"""Find all primes up to a given number, using<br />

<strong>Eratosthenes</strong>' prime sieve."""<br />

import math<br />

# sqrt<br />

size = input("Find all primes up to: ")<br />

# Initialize: all numbers except 0, 1 are prime<br />

primeFlags = range(size+1) # so pF[size] exists<br />

for num in range(size+1):<br />

primeFlags[num] = True<br />

primeFlags[0] = False<br />

primeFlags[1] = False<br />

CMPT140: <strong>Sieve</strong> <strong>of</strong> <strong>Eratosthenes</strong> 14 Oct 2009<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!