|
1 | 1 | # Instructions
|
2 | 2 |
|
3 |
| -Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find prime numbers. |
| 3 | +Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find all prime numbers less than or equal to a given number. |
4 | 4 |
|
5 |
| -A prime number is a number that is only divisible by 1 and itself. |
| 5 | +A prime number is a number larger than 1 that is only divisible by 1 and itself. |
6 | 6 | For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
|
7 |
| - |
8 |
| -The Sieve of Eratosthenes is an ancient algorithm that works by taking a list of numbers and crossing out all the numbers that aren't prime. |
9 |
| - |
10 |
| -A number that is **not** prime is called a "composite number". |
| 7 | +By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3. |
11 | 8 |
|
12 | 9 | To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number.
|
13 | 10 | Then you repeat the following steps:
|
14 | 11 |
|
15 |
| -1. Find the next unmarked number in your list. This is a prime number. |
16 |
| -2. Mark all the multiples of that prime number as composite (not prime). |
| 12 | +1. Find the next unmarked number in your list (skipping over marked numbers). |
| 13 | + This is a prime number. |
| 14 | +2. Mark all the multiples of that prime number as **not** prime. |
17 | 15 |
|
18 | 16 | You keep repeating these steps until you've gone through every number in your list.
|
19 | 17 | At the end, all the unmarked numbers are prime.
|
20 | 18 |
|
21 | 19 | ~~~~exercism/note
|
22 |
| -[Wikipedia's Sieve of Eratosthenes article][eratosthenes] has a useful graphic that explains the algorithm. |
23 |
| -
|
24 | 20 | The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes.
|
25 |
| -A good first test is to check that you do not use division or remainder operations. |
26 |
| -
|
27 |
| -[eratosthenes]: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes |
| 21 | +To check you are implementing the Sieve correctly, a good first test is to check that you do not use division or remainder operations. |
28 | 22 | ~~~~
|
| 23 | + |
| 24 | +## Example |
| 25 | + |
| 26 | +Let's say you're finding the primes less than or equal to 10. |
| 27 | + |
| 28 | +- List out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked. |
| 29 | +- 2 is unmarked and is therefore a prime. |
| 30 | + Mark 4, 6, 8 and 10 as "not prime". |
| 31 | +- 3 is unmarked and is therefore a prime. |
| 32 | + Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_. |
| 33 | +- 4 is marked as "not prime", so we skip over it. |
| 34 | +- 5 is unmarked and is therefore a prime. |
| 35 | + Mark 10 as not prime _(optional - as it's already been marked)_. |
| 36 | +- 6 is marked as "not prime", so we skip over it. |
| 37 | +- 7 is unmarked and is therefore a prime. |
| 38 | +- 8 is marked as "not prime", so we skip over it. |
| 39 | +- 9 is marked as "not prime", so we skip over it. |
| 40 | +- 10 is marked as "not prime", so we stop as there are no more numbers to check. |
| 41 | + |
| 42 | +You've examined all numbers and found 2, 3, 5, and 7 are still unmarked, which means they're the primes less than or equal to 10. |
0 commit comments