|
1 | 1 | # Instructions |
2 | 2 |
|
3 | | -Use the Sieve of Eratosthenes to find all the primes from 2 up to a given |
4 | | -number. |
| 3 | +Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find prime numbers. |
5 | 4 |
|
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. |
| 5 | +A prime number is a number that is only divisible by 1 and itself. |
| 6 | +For example, 2, 3, 5, 7, 11, and 13 are prime numbers. |
10 | 7 |
|
11 | | -Create your range, starting at two and continuing up to and including the given limit. (i.e. [2, limit]) |
| 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. |
12 | 9 |
|
13 | | -The algorithm consists of repeating the following over and over: |
| 10 | +A number that is **not** prime is called a "composite number". |
14 | 11 |
|
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) |
| 12 | +To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number. |
| 13 | +Then you repeat the following steps: |
17 | 14 |
|
18 | | -Repeat until you have processed each number in your range. |
| 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). |
19 | 17 |
|
20 | | -When the algorithm terminates, all the numbers in the list that have not |
21 | | -been marked are prime. |
| 18 | +You keep repeating these steps until you've gone through every number in your list. |
| 19 | +At the end, all the unmarked numbers are prime. |
22 | 20 |
|
23 | | -The wikipedia article has a useful graphic that explains the algorithm: |
24 | | -https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes |
| 21 | +```exercism/note |
| 22 | +[Wikipedia's Sieve of Eratosthenes article][eratosthenes] has a useful graphic that explains the algorithm. |
25 | 23 |
|
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). |
| 24 | +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 |
| 28 | +``` |
0 commit comments