aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico I <git@federicoigne.com>2020-03-17 20:34:57 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:54:52 +0000
commit581450f9b2bfba9d9ff9efdab6776b0d4fdbae14 (patch)
tree217c4f0866ed7eac9b582152d269fd3ccd21b37d
parent9975f9635cd1c2c58b6c1dbe47585859b0b1d573 (diff)
downloadexercism-581450f9b2bfba9d9ff9efdab6776b0d4fdbae14.tar.gz
exercism-581450f9b2bfba9d9ff9efdab6776b0d4fdbae14.zip
[rust] Sum of Multiples
-rw-r--r--sum-of-multiples/.exercism/metadata.json1
-rw-r--r--sum-of-multiples/.gitignore8
-rw-r--r--sum-of-multiples/Cargo.toml4
-rw-r--r--sum-of-multiples/README.md89
-rw-r--r--sum-of-multiples/src/lib.rs3
-rw-r--r--sum-of-multiples/tests/sum-of-multiples.rs81
6 files changed, 186 insertions, 0 deletions
diff --git a/sum-of-multiples/.exercism/metadata.json b/sum-of-multiples/.exercism/metadata.json
new file mode 100644
index 0000000..98ef3f1
--- /dev/null
+++ b/sum-of-multiples/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"sum-of-multiples","id":"9f1716859ebb4e38b10e5dfbd3858ad3","url":"https://exercism.io/my/solutions/9f1716859ebb4e38b10e5dfbd3858ad3","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/sum-of-multiples/.gitignore b/sum-of-multiples/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/sum-of-multiples/.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/sum-of-multiples/Cargo.toml b/sum-of-multiples/Cargo.toml
new file mode 100644
index 0000000..a006459
--- /dev/null
+++ b/sum-of-multiples/Cargo.toml
@@ -0,0 +1,4 @@
1[package]
2edition = "2018"
3name = "sum-of-multiples"
4version = "1.5.0"
diff --git a/sum-of-multiples/README.md b/sum-of-multiples/README.md
new file mode 100644
index 0000000..72bb481
--- /dev/null
+++ b/sum-of-multiples/README.md
@@ -0,0 +1,89 @@
1# Sum Of Multiples
2
3Given a number, find the sum of all the unique multiples of particular numbers up to
4but not including that number.
5
6If we list all the natural numbers below 20 that are multiples of 3 or 5,
7we get 3, 5, 6, 9, 10, 12, 15, and 18.
8
9The sum of these multiples is 78.
10
11## Rust Installation
12
13Refer to the [exercism help page][help-page] for Rust installation and learning
14resources.
15
16## Writing the Code
17
18Execute the tests with:
19
20```bash
21$ cargo test
22```
23
24All but the first test have been ignored. After you get the first test to
25pass, open the tests source file which is located in the `tests` directory
26and remove the `#[ignore]` flag from the next test and get the tests to pass
27again. Each separate test is a function with `#[test]` flag above it.
28Continue, until you pass every test.
29
30If you wish to run all ignored tests without editing the tests source file, use:
31
32```bash
33$ cargo test -- --ignored
34```
35
36To run a specific test, for example `some_test`, you can use:
37
38```bash
39$ cargo test some_test
40```
41
42If the specific test is ignored use:
43
44```bash
45$ cargo test some_test -- --ignored
46```
47
48To learn more about Rust tests refer to the [online test documentation][rust-tests]
49
50Make sure to read the [Modules][modules] chapter if you
51haven't already, it will help you with organizing your files.
52
53## Further improvements
54
55After 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.
56
57To format your solution, inside the solution directory use
58
59```bash
60cargo fmt
61```
62
63To see, if your solution contains some common ineffective use cases, inside the solution directory use
64
65```bash
66cargo clippy --all-targets
67```
68
69## Submitting the solution
70
71Generally 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.
72
73## Feedback, Issues, Pull Requests
74
75The [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!
76
77If 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).
78
79[help-page]: https://exercism.io/tracks/rust/learning
80[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
81[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
82[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
83
84## Source
85
86A variation on Problem 1 at Project Euler [http://projecteuler.net/problem=1](http://projecteuler.net/problem=1)
87
88## Submitting Incomplete Solutions
89It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/sum-of-multiples/src/lib.rs b/sum-of-multiples/src/lib.rs
new file mode 100644
index 0000000..148419d
--- /dev/null
+++ b/sum-of-multiples/src/lib.rs
@@ -0,0 +1,3 @@
1pub fn sum_of_multiples(limit: u32, factors: &[u32]) -> u32 {
2 (1..limit).filter(|x| factors.iter().filter(|&&x| x != 0).any(|y| x % y == 0)).sum()
3}
diff --git a/sum-of-multiples/tests/sum-of-multiples.rs b/sum-of-multiples/tests/sum-of-multiples.rs
new file mode 100644
index 0000000..369289c
--- /dev/null
+++ b/sum-of-multiples/tests/sum-of-multiples.rs
@@ -0,0 +1,81 @@
1use sum_of_multiples::*;
2
3#[test]
4fn no_multiples_within_limit() {
5 assert_eq!(0, sum_of_multiples(1, &[3, 5]))
6}
7
8#[test]
9fn one_factor_has_multiples_within_limit() {
10 assert_eq!(3, sum_of_multiples(4, &[3, 5]))
11}
12
13#[test]
14fn more_than_one_multiple_within_limit() {
15 assert_eq!(9, sum_of_multiples(7, &[3]))
16}
17
18#[test]
19fn more_than_one_factor_with_multiples_within_limit() {
20 assert_eq!(23, sum_of_multiples(10, &[3, 5]))
21}
22
23#[test]
24fn each_multiple_is_only_counted_once() {
25 assert_eq!(2318, sum_of_multiples(100, &[3, 5]))
26}
27
28#[test]
29fn a_much_larger_limit() {
30 assert_eq!(233_168, sum_of_multiples(1000, &[3, 5]))
31}
32
33#[test]
34fn three_factors() {
35 assert_eq!(51, sum_of_multiples(20, &[7, 13, 17]))
36}
37
38#[test]
39fn factors_not_relatively_prime() {
40 assert_eq!(30, sum_of_multiples(15, &[4, 6]))
41}
42
43#[test]
44fn some_pairs_of_factors_relatively_prime_and_some_not() {
45 assert_eq!(4419, sum_of_multiples(150, &[5, 6, 8]))
46}
47
48#[test]
49fn one_factor_is_a_multiple_of_another() {
50 assert_eq!(275, sum_of_multiples(51, &[5, 25]))
51}
52
53#[test]
54fn much_larger_factors() {
55 assert_eq!(2_203_160, sum_of_multiples(10_000, &[43, 47]))
56}
57
58#[test]
59fn all_numbers_are_multiples_of_1() {
60 assert_eq!(4950, sum_of_multiples(100, &[1]))
61}
62
63#[test]
64fn no_factors_means_an_empty_sum() {
65 assert_eq!(0, sum_of_multiples(10_000, &[]))
66}
67
68#[test]
69fn the_only_multiple_of_0_is_0() {
70 assert_eq!(0, sum_of_multiples(1, &[0]))
71}
72
73#[test]
74fn the_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors() {
75 assert_eq!(3, sum_of_multiples(4, &[3, 0]))
76}
77
78#[test]
79fn solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3() {
80 assert_eq!(39_614_537, sum_of_multiples(10_000, &[2, 3, 5, 7, 11]))
81}