diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:48:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch) | |
| tree | 8e39798fcaf27931d91c2088423fd4e97adcfc2d /rust/prime-factors | |
| parent | 4e2052c4d792540c2f742b2c2a081b11117ed41d (diff) | |
| download | exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip | |
Move Rust exercises in a subdirectory
Diffstat (limited to 'rust/prime-factors')
| -rw-r--r-- | rust/prime-factors/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | rust/prime-factors/.gitignore | 8 | ||||
| -rw-r--r-- | rust/prime-factors/Cargo.toml | 6 | ||||
| -rw-r--r-- | rust/prime-factors/README.md | 110 | ||||
| -rw-r--r-- | rust/prime-factors/src/lib.rs | 18 | ||||
| -rw-r--r-- | rust/prime-factors/tests/prime-factors.rs | 36 |
6 files changed, 179 insertions, 0 deletions
diff --git a/rust/prime-factors/.exercism/metadata.json b/rust/prime-factors/.exercism/metadata.json new file mode 100644 index 0000000..293742a --- /dev/null +++ b/rust/prime-factors/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"prime-factors","id":"a0a637c86ddc4aa2b63cfe05a163292d","url":"https://exercism.io/my/solutions/a0a637c86ddc4aa2b63cfe05a163292d","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/rust/prime-factors/.gitignore b/rust/prime-factors/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/prime-factors/.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/prime-factors/Cargo.toml b/rust/prime-factors/Cargo.toml new file mode 100644 index 0000000..70727e6 --- /dev/null +++ b/rust/prime-factors/Cargo.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "prime_factors" | ||
| 4 | version = "1.1.0" | ||
| 5 | |||
| 6 | [dependencies] | ||
diff --git a/rust/prime-factors/README.md b/rust/prime-factors/README.md new file mode 100644 index 0000000..f106dde --- /dev/null +++ b/rust/prime-factors/README.md | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | # Prime Factors | ||
| 2 | |||
| 3 | Compute the prime factors of a given natural number. | ||
| 4 | |||
| 5 | A prime number is only evenly divisible by itself and 1. | ||
| 6 | |||
| 7 | Note that 1 is not a prime number. | ||
| 8 | |||
| 9 | ## Example | ||
| 10 | |||
| 11 | What are the prime factors of 60? | ||
| 12 | |||
| 13 | - Our first divisor is 2. 2 goes into 60, leaving 30. | ||
| 14 | - 2 goes into 30, leaving 15. | ||
| 15 | - 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3. | ||
| 16 | - 3 goes cleanly into 15, leaving 5. | ||
| 17 | - 3 does not go cleanly into 5. The next possible factor is 4. | ||
| 18 | - 4 does not go cleanly into 5. The next possible factor is 5. | ||
| 19 | - 5 does go cleanly into 5. | ||
| 20 | - We're left only with 1, so now, we're done. | ||
| 21 | |||
| 22 | Our successful divisors in that computation represent the list of prime | ||
| 23 | factors of 60: 2, 2, 3, and 5. | ||
| 24 | |||
| 25 | You can check this yourself: | ||
| 26 | |||
| 27 | - 2 * 2 * 3 * 5 | ||
| 28 | - = 4 * 15 | ||
| 29 | - = 60 | ||
| 30 | - Success! | ||
| 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 | The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata) | ||
| 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/prime-factors/src/lib.rs b/rust/prime-factors/src/lib.rs new file mode 100644 index 0000000..3d7b41f --- /dev/null +++ b/rust/prime-factors/src/lib.rs | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | |||
| 2 | fn factors_aux(mut vec: Vec<u64>, n: u64) -> Vec<u64> { | ||
| 3 | match n { | ||
| 4 | 1 => vec, | ||
| 5 | n if n % 2 == 0 => { | ||
| 6 | factors_aux({ vec.push(2); vec }, n / 2) | ||
| 7 | }, | ||
| 8 | _ => { | ||
| 9 | let mut i: u64 = 3; | ||
| 10 | while n % i != 0 { i += 2 } | ||
| 11 | factors_aux({ vec.push(i); vec }, n / i) | ||
| 12 | } | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | pub fn factors(n: u64) -> Vec<u64> { | ||
| 17 | factors_aux(vec![],n) | ||
| 18 | } | ||
diff --git a/rust/prime-factors/tests/prime-factors.rs b/rust/prime-factors/tests/prime-factors.rs new file mode 100644 index 0000000..1042415 --- /dev/null +++ b/rust/prime-factors/tests/prime-factors.rs | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | use prime_factors::factors; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn test_no_factors() { | ||
| 5 | assert_eq!(factors(1), vec![]); | ||
| 6 | } | ||
| 7 | |||
| 8 | #[test] | ||
| 9 | fn test_prime_number() { | ||
| 10 | assert_eq!(factors(2), vec![2]); | ||
| 11 | } | ||
| 12 | |||
| 13 | #[test] | ||
| 14 | fn test_square_of_a_prime() { | ||
| 15 | assert_eq!(factors(9), vec![3, 3]); | ||
| 16 | } | ||
| 17 | |||
| 18 | #[test] | ||
| 19 | fn test_cube_of_a_prime() { | ||
| 20 | assert_eq!(factors(8), vec![2, 2, 2]); | ||
| 21 | } | ||
| 22 | |||
| 23 | #[test] | ||
| 24 | fn test_product_of_primes_and_non_primes() { | ||
| 25 | assert_eq!(factors(12), vec![2, 2, 3]); | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn test_product_of_primes() { | ||
| 30 | assert_eq!(factors(901_255), vec![5, 17, 23, 461]); | ||
| 31 | } | ||
| 32 | |||
| 33 | #[test] | ||
| 34 | fn test_factors_include_large_prime() { | ||
| 35 | assert_eq!(factors(93_819_012_551), vec![11, 9539, 894_119]); | ||
| 36 | } | ||
