Week 2.2. Exercise Class - Sieve Of Eratosthenes
Week 2.2. Exercise Class - Sieve of Eratosthenes
What is an algorithm?
An algorithm is a systematic way of solving a problem.
We have seen some already:
- A recipe for a sponge cake
- Calculating quadratic roots using a formula
These are very simple algorithms. We will look at a complicted one and write the code.
You may not understand the code in detail yet. The systematic approach and the high-level idea are important.
The sieve of Eratosthenes
- Eratosthenes was a Greek mathematician (276 BC - 194 BC)
- He notably designed an algorithm for calculating primes.
The idea:
- Start with a list of numbers from 2 to some bound.
- The first number in the list is prime.
- Mark this number as prime.
- Cross out all multiples of this number.
- Repeat until everything is crossed out.
Running the algorithm
- Start with a list of numbers from 2 to some bound (here bound = 55)
- The first number in the list is prime
a. mark it as prime
b. cross out all multiples of that number - Repeat the above (2 -> 3) until everything is crossed out.