|
1 | 1 | # Instructions
|
2 | 2 |
|
3 |
| -Use the Sieve of Eratosthenes to find all the primes from 2 up to a given |
4 |
| -number. |
5 |
| - |
6 |
| -The Sieve of Eratosthenes is a simple, ancient algorithm for finding all |
7 |
| -prime numbers up to any given limit. It does so by iteratively marking as |
8 |
| -composite (i.e. not prime) the multiples of each prime, starting with the |
9 |
| -multiples of 2. It does not use any division or remainder operation. |
10 |
| - |
11 |
| -Create your range, starting at two and continuing up to and including the given limit. (i.e. [2, limit]) |
12 |
| - |
13 |
| -The algorithm consists of repeating the following over and over: |
14 |
| - |
15 |
| -- take the next available unmarked number in your list (it is prime) |
16 |
| -- mark all the multiples of that number (they are not prime) |
17 |
| - |
18 |
| -Repeat until you have processed each number in your range. |
19 |
| - |
20 |
| -When the algorithm terminates, all the numbers in the list that have not |
21 |
| -been marked are prime. |
22 |
| - |
23 |
| -The wikipedia article has a useful graphic that explains the algorithm: |
24 |
| -[https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) |
25 |
| - |
26 |
| -Notice that this is a very specific algorithm, and the tests don't check |
27 |
| -that you've implemented the algorithm, only that you've come up with the |
28 |
| -correct list of primes. A good first test is to check that you do not use |
29 |
| -division or remainder operations (div, /, mod or % depending on the |
30 |
| -language). |
| 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 | + |
| 5 | +A prime number is a number larger than 1 that is only divisible by 1 and itself. |
| 6 | +For example, 2, 3, 5, 7, 11, and 13 are prime numbers. |
| 7 | +By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3. |
| 8 | + |
| 9 | +To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number. |
| 10 | +Then you repeat the following steps: |
| 11 | + |
| 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. |
| 15 | + |
| 16 | +You keep repeating these steps until you've gone through every number in your list. |
| 17 | +At the end, all the unmarked numbers are prime. |
| 18 | + |
| 19 | +~~~~exercism/note |
| 20 | +The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes. |
| 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. |
| 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