From c38a0dc046745e323bd9c3b83080a05bb611bd10 Mon Sep 17 00:00:00 2001 From: Michael Kamau Date: Thu, 6 Sep 2018 01:46:37 +0300 Subject: [PATCH 1/2] Add arithmetic series solution --- src/bin/1.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/bin/1.rs b/src/bin/1.rs index b9d5945..1cd8e8e 100644 --- a/src/bin/1.rs +++ b/src/bin/1.rs @@ -1,6 +1,7 @@ /// Problem 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. +/// 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. /// /// Find the sum of all the multiples of 3 or 5 below 1000. @@ -10,14 +11,50 @@ fn sum_multiples_3_5(n: i32) -> i32 { sum } -fn main() { - println!("the sum of all the multiples of 3 or 5 below 10: {}", sum_multiples_3_5(10)); - println!("the sum of all the multiples of 3 or 5 below 1000: {}", sum_multiples_3_5(1000)); +/// Calculates and returns the sum of multiples of a `num` +/// below the `limit` using arithmetic series +fn sum_multiples(num: i32, limit: i32) -> i32 { + let num_of_multiples = (limit - 1) / num; + num * ((num_of_multiples * (num_of_multiples + 1)) / 2) +} + +/// Calculates and returns the sum of all the multiples of 3 or 5 below 1000. +/// using the helper function ```sum_multiples``` +pub fn problem_1_sol_2(limit: i32) -> i32 { + let sum_3 = sum_multiples(3, limit); + let sum_5 = sum_multiples(5, limit); + let sum_15 = sum_multiples(15, limit); + sum_3 + sum_5 - sum_15 } +fn main() { + println!( + "the sum of all the multiples of 3 or 5 below 10: {}", + sum_multiples_3_5(10) + ); + println!( + "the sum of all the multiples of 3 or 5 below 1000: {}", + sum_multiples_3_5(1000) + ); + print!("Using problem_1_sol_2:"); + print!( + "\tthe sum of all the multiples of 3 or 5 below 1000: {}", + problem_1_sol_2(1000) + ); +} #[test] fn test() { assert_eq!(sum_multiples_3_5(10), 23); assert_eq!(sum_multiples_3_5(1000), 233168); } + +#[test] +fn sum_of_multiples_3_below_10() { + assert_eq!(18, sum_multiples(3, 10)); +} + +#[test] +fn test_sum_below_10() { + assert_eq!(23, problem_1_sol_2(10)); +} From b1aa3da85fb26cfa818a2415eb58e802494df7a6 Mon Sep 17 00:00:00 2001 From: Michael Kamau Date: Sat, 27 Oct 2018 12:36:37 +0300 Subject: [PATCH 2/2] update name --- src/bin/1.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bin/1.rs b/src/bin/1.rs index 1cd8e8e..49f7114 100644 --- a/src/bin/1.rs +++ b/src/bin/1.rs @@ -5,7 +5,7 @@ /// /// Find the sum of all the multiples of 3 or 5 below 1000. -fn sum_multiples_3_5(n: i32) -> i32 { +fn problem_1_sol_1(n: i32) -> i32 { let sum = (0..n).filter(|x| x % 3 == 0 || x % 5 == 0).sum(); sum @@ -30,11 +30,11 @@ pub fn problem_1_sol_2(limit: i32) -> i32 { fn main() { println!( "the sum of all the multiples of 3 or 5 below 10: {}", - sum_multiples_3_5(10) + problem_1_sol_1(10) ); println!( "the sum of all the multiples of 3 or 5 below 1000: {}", - sum_multiples_3_5(1000) + problem_1_sol_1(1000) ); print!("Using problem_1_sol_2:"); print!( @@ -45,8 +45,8 @@ fn main() { #[test] fn test() { - assert_eq!(sum_multiples_3_5(10), 23); - assert_eq!(sum_multiples_3_5(1000), 233168); + assert_eq!(problem_1_sol_1(10), 23); + assert_eq!(problem_1_sol_1(1000), 233168); } #[test]