diff options
| -rw-r--r-- | rust/sieve/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | rust/sieve/.gitignore | 8 | ||||
| -rw-r--r-- | rust/sieve/Cargo.toml | 4 | ||||
| -rw-r--r-- | rust/sieve/README.md | 110 | ||||
| -rw-r--r-- | rust/sieve/src/lib.rs | 16 | ||||
| -rw-r--r-- | rust/sieve/tests/sieve.rs | 35 |
6 files changed, 174 insertions, 0 deletions
diff --git a/rust/sieve/.exercism/metadata.json b/rust/sieve/.exercism/metadata.json new file mode 100644 index 0000000..8509220 --- /dev/null +++ b/rust/sieve/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"sieve","id":"9d898407ffbd41c6a50ec8bb91efec07","url":"https://exercism.io/my/solutions/9d898407ffbd41c6a50ec8bb91efec07","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/rust/sieve/.gitignore b/rust/sieve/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/sieve/.gitignore | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # Generated by Cargo | ||
| 2 | # will have compiled files and executables | ||
| 3 | /target/ | ||
| 4 | **/*.rs.bk | ||
| 5 | |||
| 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
| 7 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| 8 | Cargo.lock | ||
diff --git a/rust/sieve/Cargo.toml b/rust/sieve/Cargo.toml new file mode 100644 index 0000000..3af9123 --- /dev/null +++ b/rust/sieve/Cargo.toml | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "sieve" | ||
| 4 | version = "1.1.0" | ||
diff --git a/rust/sieve/README.md b/rust/sieve/README.md new file mode 100644 index 0000000..8e01d40 --- /dev/null +++ b/rust/sieve/README.md | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | # Sieve | ||
| 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 | ||
| 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). | ||
| 31 | |||
| 32 | ## Rust Installation | ||
| 33 | |||
| 34 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 35 | resources. | ||
| 36 | |||
| 37 | ## Writing the Code | ||
| 38 | |||
| 39 | Execute the tests with: | ||
| 40 | |||
| 41 | ```bash | ||
| 42 | $ cargo test | ||
| 43 | ``` | ||
| 44 | |||
| 45 | All but the first test have been ignored. After you get the first test to | ||
| 46 | pass, open the tests source file which is located in the `tests` directory | ||
| 47 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 48 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 49 | Continue, until you pass every test. | ||
| 50 | |||
| 51 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 52 | |||
| 53 | ```bash | ||
| 54 | $ cargo test -- --ignored | ||
| 55 | ``` | ||
| 56 | |||
| 57 | To run a specific test, for example `some_test`, you can use: | ||
| 58 | |||
| 59 | ```bash | ||
| 60 | $ cargo test some_test | ||
| 61 | ``` | ||
| 62 | |||
| 63 | If the specific test is ignored use: | ||
| 64 | |||
| 65 | ```bash | ||
| 66 | $ cargo test some_test -- --ignored | ||
| 67 | ``` | ||
| 68 | |||
| 69 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 70 | |||
| 71 | Make sure to read the [Modules][modules] chapter if you | ||
| 72 | haven't already, it will help you with organizing your files. | ||
| 73 | |||
| 74 | ## Further improvements | ||
| 75 | |||
| 76 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 77 | |||
| 78 | To format your solution, inside the solution directory use | ||
| 79 | |||
| 80 | ```bash | ||
| 81 | cargo fmt | ||
| 82 | ``` | ||
| 83 | |||
| 84 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 85 | |||
| 86 | ```bash | ||
| 87 | cargo clippy --all-targets | ||
| 88 | ``` | ||
| 89 | |||
| 90 | ## Submitting the solution | ||
| 91 | |||
| 92 | Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. | ||
| 93 | |||
| 94 | ## Feedback, Issues, Pull Requests | ||
| 95 | |||
| 96 | The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! | ||
| 97 | |||
| 98 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 99 | |||
| 100 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 101 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 102 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 103 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 104 | |||
| 105 | ## Source | ||
| 106 | |||
| 107 | Sieve of Eratosthenes at Wikipedia [http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | ||
| 108 | |||
| 109 | ## Submitting Incomplete Solutions | ||
| 110 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/rust/sieve/src/lib.rs b/rust/sieve/src/lib.rs new file mode 100644 index 0000000..307ff83 --- /dev/null +++ b/rust/sieve/src/lib.rs | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | pub fn primes_up_to(upper_bound: u64) -> Vec<u64> { | ||
| 2 | let mut sieve: Vec<Option<u64>> = (2..=upper_bound).map(Some).collect(); | ||
| 3 | primes(&mut sieve); | ||
| 4 | sieve.iter().filter_map(|x| *x).collect() | ||
| 5 | } | ||
| 6 | |||
| 7 | fn primes(ps: &mut [Option<u64>]) { | ||
| 8 | if let Some(&n) = ps.first() { | ||
| 9 | if let Some(p) = n { | ||
| 10 | (p as usize..ps.len()) | ||
| 11 | .step_by(p as usize) | ||
| 12 | .for_each(|x| ps[x] = None) | ||
| 13 | } | ||
| 14 | primes(&mut ps[1..]); | ||
| 15 | } | ||
| 16 | } | ||
diff --git a/rust/sieve/tests/sieve.rs b/rust/sieve/tests/sieve.rs new file mode 100644 index 0000000..eb8fcb6 --- /dev/null +++ b/rust/sieve/tests/sieve.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #[test] | ||
| 2 | fn limit_lower_than_the_first_prime() { | ||
| 3 | assert_eq!(sieve::primes_up_to(1), []); | ||
| 4 | } | ||
| 5 | |||
| 6 | #[test] | ||
| 7 | fn limit_is_the_first_prime() { | ||
| 8 | assert_eq!(sieve::primes_up_to(2), [2]); | ||
| 9 | } | ||
| 10 | |||
| 11 | #[test] | ||
| 12 | fn primes_up_to_10() { | ||
| 13 | assert_eq!(sieve::primes_up_to(10), [2, 3, 5, 7]); | ||
| 14 | } | ||
| 15 | |||
| 16 | #[test] | ||
| 17 | fn limit_is_prime() { | ||
| 18 | assert_eq!(sieve::primes_up_to(13), [2, 3, 5, 7, 11, 13]); | ||
| 19 | } | ||
| 20 | |||
| 21 | #[test] | ||
| 22 | fn limit_of_1000() { | ||
| 23 | let expected = vec![ | ||
| 24 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, | ||
| 25 | 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, | ||
| 26 | 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, | ||
| 27 | 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, | ||
| 28 | 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, | ||
| 29 | 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, | ||
| 30 | 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, | ||
| 31 | 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, | ||
| 32 | 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, | ||
| 33 | ]; | ||
| 34 | assert_eq!(sieve::primes_up_to(1000), expected); | ||
| 35 | } | ||
