There is a git repository on Github that contains 100 ideas for programming projects. Most of them aren't too difficult. I will occasionally do one or two projects to keep my programming skills sharp, and maybe learn a thing or two.
Today, I decided to implement the Sieve of Erathosthenes from the list of projects. I followed these instructions from Wikipedia:
To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
- Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
- Initially, let p equal 2, the first prime number.
- Starting from p, enumerate its multiples by counting to n in increments of p, and mark them in the list (these will be 2p, 3p, 4p, etc.; the p itself should not be marked).
- Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.
Using these instructions as a guide, I translated everything to Python. I used a dictionary to keep track of the primes. Any key that had a value of False, is not a prime: