Skip to content

Commit a6f5718

Browse files
committed
Solution to rust/sum-of-multiples
1 parent 3c291ce commit a6f5718

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

rust/sum-of-multiples/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

rust/sum-of-multiples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2021"
3+
name = "sum-of-multiples"
4+
version = "1.5.0"

rust/sum-of-multiples/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Sum of Multiples
2+
3+
Welcome to Sum of Multiples on Exercism's Rust Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Instructions
7+
8+
Given a list of factors and a limit, add up all the unique multiples of the factors that are less than the limit.
9+
All inputs will be greater than or equal to zero.
10+
11+
## Example
12+
13+
Suppose the limit is 20 and the list of factors is [3, 5].
14+
We need to find the sum of all unique multiples of 3 and 5 that are less than 20.
15+
16+
Multiples of 3 less than 20: 3, 6, 9, 12, 15, 18
17+
Multiples of 5 less than 20: 5, 10, 15
18+
19+
The unique multiples are: 3, 5, 6, 9, 10, 12, 15, 18
20+
21+
The sum of the unique multiples is: 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78
22+
23+
So, the answer is 78.
24+
25+
## Source
26+
27+
### Created by
28+
29+
- @IanWhitney
30+
31+
### Contributed to by
32+
33+
- @coriolinus
34+
- @cwhakes
35+
- @eddyp
36+
- @efx
37+
- @ErikSchierboom
38+
- @GoneUp
39+
- @hekrause
40+
- @leoyvens
41+
- @lutostag
42+
- @mkantor
43+
- @nfiles
44+
- @petertseng
45+
- @rofrol
46+
- @sshine
47+
- @stringparser
48+
- @xakon
49+
- @ZapAnton
50+
51+
### Based on
52+
53+
A variation on Problem 1 at Project Euler - http://projecteuler.net/problem=1

rust/sum-of-multiples/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn sum_of_multiples(limit: u32, factors: &[u32]) -> u32 {
2+
(1..limit)
3+
.filter(|n| factors.iter().any(|f| f != &0 && n % f == 0))
4+
.sum()
5+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use sum_of_multiples::*;
2+
3+
#[test]
4+
fn no_multiples_within_limit() {
5+
assert_eq!(0, sum_of_multiples(1, &[3, 5]))
6+
}
7+
8+
#[test]
9+
#[ignore]
10+
fn one_factor_has_multiples_within_limit() {
11+
assert_eq!(3, sum_of_multiples(4, &[3, 5]))
12+
}
13+
14+
#[test]
15+
#[ignore]
16+
fn more_than_one_multiple_within_limit() {
17+
assert_eq!(9, sum_of_multiples(7, &[3]))
18+
}
19+
20+
#[test]
21+
#[ignore]
22+
fn more_than_one_factor_with_multiples_within_limit() {
23+
assert_eq!(23, sum_of_multiples(10, &[3, 5]))
24+
}
25+
26+
#[test]
27+
#[ignore]
28+
fn each_multiple_is_only_counted_once() {
29+
assert_eq!(2318, sum_of_multiples(100, &[3, 5]))
30+
}
31+
32+
#[test]
33+
#[ignore]
34+
fn a_much_larger_limit() {
35+
assert_eq!(233_168, sum_of_multiples(1000, &[3, 5]))
36+
}
37+
38+
#[test]
39+
#[ignore]
40+
fn three_factors() {
41+
assert_eq!(51, sum_of_multiples(20, &[7, 13, 17]))
42+
}
43+
44+
#[test]
45+
#[ignore]
46+
fn factors_not_relatively_prime() {
47+
assert_eq!(30, sum_of_multiples(15, &[4, 6]))
48+
}
49+
50+
#[test]
51+
#[ignore]
52+
fn some_pairs_of_factors_relatively_prime_and_some_not() {
53+
assert_eq!(4419, sum_of_multiples(150, &[5, 6, 8]))
54+
}
55+
56+
#[test]
57+
#[ignore]
58+
fn one_factor_is_a_multiple_of_another() {
59+
assert_eq!(275, sum_of_multiples(51, &[5, 25]))
60+
}
61+
62+
#[test]
63+
#[ignore]
64+
fn much_larger_factors() {
65+
assert_eq!(2_203_160, sum_of_multiples(10_000, &[43, 47]))
66+
}
67+
68+
#[test]
69+
#[ignore]
70+
fn all_numbers_are_multiples_of_1() {
71+
assert_eq!(4950, sum_of_multiples(100, &[1]))
72+
}
73+
74+
#[test]
75+
#[ignore]
76+
fn no_factors_means_an_empty_sum() {
77+
assert_eq!(0, sum_of_multiples(10_000, &[]))
78+
}
79+
80+
#[test]
81+
#[ignore]
82+
fn the_only_multiple_of_0_is_0() {
83+
assert_eq!(0, sum_of_multiples(1, &[0]))
84+
}
85+
86+
#[test]
87+
#[ignore]
88+
fn the_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors() {
89+
assert_eq!(3, sum_of_multiples(4, &[3, 0]))
90+
}
91+
92+
#[test]
93+
#[ignore]
94+
fn solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3() {
95+
assert_eq!(39_614_537, sum_of_multiples(10_000, &[2, 3, 5, 7, 11]))
96+
}

0 commit comments

Comments
 (0)