-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Sync the `sieve` exercise's docs with the latest data. * Sync the `sieve` exercise's metadata with the latest data.
- Loading branch information
1 parent
9fb74f4
commit 79c3b6c
Showing
2 changed files
with
28 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
# Instructions | ||
|
||
Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find prime numbers. | ||
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. | ||
|
||
A prime number is a number that is only divisible by 1 and itself. | ||
A prime number is a number larger than 1 that is only divisible by 1 and itself. | ||
For example, 2, 3, 5, 7, 11, and 13 are prime numbers. | ||
|
||
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. | ||
|
||
A number that is **not** prime is called a "composite number". | ||
By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3. | ||
|
||
To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number. | ||
Then you repeat the following steps: | ||
|
||
1. Find the next unmarked number in your list. This is a prime number. | ||
2. Mark all the multiples of that prime number as composite (not prime). | ||
1. Find the next unmarked number in your list (skipping over marked numbers). | ||
This is a prime number. | ||
2. Mark all the multiples of that prime number as **not** prime. | ||
|
||
You keep repeating these steps until you've gone through every number in your list. | ||
At the end, all the unmarked numbers are prime. | ||
|
||
~~~~exercism/note | ||
[Wikipedia's Sieve of Eratosthenes article][eratosthenes] has a useful graphic that explains the algorithm. | ||
The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes. | ||
A good first test is to check that you do not use division or remainder operations. | ||
[eratosthenes]: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | ||
To check you are implementing the Sieve correctly, a good first test is to check that you do not use division or remainder operations. | ||
~~~~ | ||
|
||
## Example | ||
|
||
Let's say you're finding the primes less than or equal to 10. | ||
|
||
- List out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked. | ||
- 2 is unmarked and is therefore a prime. | ||
Mark 4, 6, 8 and 10 as "not prime". | ||
- 3 is unmarked and is therefore a prime. | ||
Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_. | ||
- 4 is marked as "not prime", so we skip over it. | ||
- 5 is unmarked and is therefore a prime. | ||
Mark 10 as not prime _(optional - as it's already been marked)_. | ||
- 6 is marked as "not prime", so we skip over it. | ||
- 7 is unmarked and is therefore a prime. | ||
- 8 is marked as "not prime", so we skip over it. | ||
- 9 is marked as "not prime", so we skip over it. | ||
- 10 is marked as "not prime", so we stop as there are no more numbers to check. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters