Skip to content

Commit 108ed5d

Browse files
m1
0 parents  commit 108ed5d

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "project-euler"
3+
version = "0.1.0"
4+
authors = ["Ryoji <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```
2+
cargo new project-euler --lib
3+
touch src/main.rs
4+
```

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub mod m1;
2+
pub mod m2;
3+
4+
#[cfg(test)]
5+
mod tests {
6+
#[test]
7+
fn it_works() {
8+
assert_eq!(2 + 2, 4);
9+
}
10+
}

src/m1.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
2+
///
3+
/// Find the sum of all the multiples of 3 or 5 below 1000.
4+
///
5+
/// ```rust
6+
/// use self::project_euler::m1::sum_of_all_the_multiples_of_3_or_5_below_1000;
7+
/// assert_eq!(sum_of_all_the_multiples_of_3_or_5_below_1000(), 233168);
8+
/// ```
9+
pub fn sum_of_all_the_multiples_of_3_or_5_below_1000() -> i32 {
10+
let mut sum = 0;
11+
for x in 0..1000 {
12+
if x % 3 == 0 || x % 5 == 0 {
13+
sum += x;
14+
}
15+
}
16+
sum
17+
}
18+
19+
/// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
20+
///
21+
/// Find the sum of all the multiples of 3 or 5 below 1000.
22+
///
23+
/// ```rust
24+
/// use self::project_euler::m1::sum_of_all_the_multiples_of_3_or_5_below_1000_iter;
25+
/// assert_eq!(sum_of_all_the_multiples_of_3_or_5_below_1000_iter(), 233168);
26+
/// ```
27+
pub fn sum_of_all_the_multiples_of_3_or_5_below_1000_iter() -> i32 {
28+
(0..1000).filter(|&x| x % 5 == 0 || x % 3 == 0).sum()
29+
}
30+
31+
/// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
32+
///
33+
/// Find the sum of all the multiples of 3 or 5 below 1000.
34+
///
35+
/// ```rust
36+
/// use self::project_euler::m1::sum_of_all_the_multiples_of_3_or_5_below_1000_arithmetic_series;
37+
/// assert_eq!(sum_of_all_the_multiples_of_3_or_5_below_1000_arithmetic_series(), 233168);
38+
/// ```
39+
pub fn sum_of_all_the_multiples_of_3_or_5_below_1000_arithmetic_series() -> i32 {
40+
// 0 3 6 9 .. 999 = 3 * [0 1 2 3 ... 333] = 3 * (0+333 #doubling) * 334#n / 2#halving
41+
// 0 5 10 15 .. 995 = 5 * [0 1 2 3 ... 199] = 5 * (0+199 #doubling) * 200#n / 2#halving
42+
// 0 15 30 45 .. 999-999%15#last = 15 * [0 1 2 3 ... last/15]
43+
let arithmetic_series = |common_difference: i32| -> i32 {
44+
let last = 999 - 999 % common_difference;
45+
let n = last / common_difference + 1;
46+
last * n / 2
47+
};
48+
arithmetic_series(3) + arithmetic_series(5) - arithmetic_series(15)
49+
}

src/m2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

0 commit comments

Comments
 (0)