aboutsummaryrefslogtreecommitdiff
path: root/prime-factors
diff options
context:
space:
mode:
Diffstat (limited to 'prime-factors')
-rw-r--r--prime-factors/.exercism/metadata.json1
-rw-r--r--prime-factors/.gitignore8
-rw-r--r--prime-factors/Cargo.toml6
-rw-r--r--prime-factors/README.md110
-rw-r--r--prime-factors/src/lib.rs18
-rw-r--r--prime-factors/tests/prime-factors.rs36
6 files changed, 179 insertions, 0 deletions
diff --git a/prime-factors/.exercism/metadata.json b/prime-factors/.exercism/metadata.json
new file mode 100644
index 0000000..293742a
--- /dev/null
+++ b/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/prime-factors/.gitignore b/prime-factors/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/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
8Cargo.lock
diff --git a/prime-factors/Cargo.toml b/prime-factors/Cargo.toml
new file mode 100644
index 0000000..70727e6
--- /dev/null
+++ b/prime-factors/Cargo.toml
@@ -0,0 +1,6 @@
1[package]
2edition = "2018"
3name = "prime_factors"
4version = "1.1.0"
5
6[dependencies]
diff --git a/prime-factors/README.md b/prime-factors/README.md
new file mode 100644
index 0000000..f106dde
--- /dev/null
+++ b/prime-factors/README.md
@@ -0,0 +1,110 @@
1# Prime Factors
2
3Compute the prime factors of a given natural number.
4
5A prime number is only evenly divisible by itself and 1.
6
7Note that 1 is not a prime number.
8
9## Example
10
11What 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
22Our successful divisors in that computation represent the list of prime
23factors of 60: 2, 2, 3, and 5.
24
25You can check this yourself:
26
27- 2 * 2 * 3 * 5
28- = 4 * 15
29- = 60
30- Success!
31
32## Rust Installation
33
34Refer to the [exercism help page][help-page] for Rust installation and learning
35resources.
36
37## Writing the Code
38
39Execute the tests with:
40
41```bash
42$ cargo test
43```
44
45All but the first test have been ignored. After you get the first test to
46pass, open the tests source file which is located in the `tests` directory
47and remove the `#[ignore]` flag from the next test and get the tests to pass
48again. Each separate test is a function with `#[test]` flag above it.
49Continue, until you pass every test.
50
51If you wish to run all ignored tests without editing the tests source file, use:
52
53```bash
54$ cargo test -- --ignored
55```
56
57To run a specific test, for example `some_test`, you can use:
58
59```bash
60$ cargo test some_test
61```
62
63If the specific test is ignored use:
64
65```bash
66$ cargo test some_test -- --ignored
67```
68
69To learn more about Rust tests refer to the [online test documentation][rust-tests]
70
71Make sure to read the [Modules][modules] chapter if you
72haven't already, it will help you with organizing your files.
73
74## Further improvements
75
76After 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
78To format your solution, inside the solution directory use
79
80```bash
81cargo fmt
82```
83
84To see, if your solution contains some common ineffective use cases, inside the solution directory use
85
86```bash
87cargo clippy --all-targets
88```
89
90## Submitting the solution
91
92Generally 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
96The [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
98If 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
107The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata)
108
109## Submitting Incomplete Solutions
110It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/prime-factors/src/lib.rs b/prime-factors/src/lib.rs
new file mode 100644
index 0000000..3d7b41f
--- /dev/null
+++ b/prime-factors/src/lib.rs
@@ -0,0 +1,18 @@
1
2fn 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
16pub fn factors(n: u64) -> Vec<u64> {
17 factors_aux(vec![],n)
18}
diff --git a/prime-factors/tests/prime-factors.rs b/prime-factors/tests/prime-factors.rs
new file mode 100644
index 0000000..1042415
--- /dev/null
+++ b/prime-factors/tests/prime-factors.rs
@@ -0,0 +1,36 @@
1use prime_factors::factors;
2
3#[test]
4fn test_no_factors() {
5 assert_eq!(factors(1), vec![]);
6}
7
8#[test]
9fn test_prime_number() {
10 assert_eq!(factors(2), vec![2]);
11}
12
13#[test]
14fn test_square_of_a_prime() {
15 assert_eq!(factors(9), vec![3, 3]);
16}
17
18#[test]
19fn test_cube_of_a_prime() {
20 assert_eq!(factors(8), vec![2, 2, 2]);
21}
22
23#[test]
24fn test_product_of_primes_and_non_primes() {
25 assert_eq!(factors(12), vec![2, 2, 3]);
26}
27
28#[test]
29fn test_product_of_primes() {
30 assert_eq!(factors(901_255), vec![5, 17, 23, 461]);
31}
32
33#[test]
34fn test_factors_include_large_prime() {
35 assert_eq!(factors(93_819_012_551), vec![11, 9539, 894_119]);
36}