From c1dfb112db9f33d9230acb515aaed4e2044907b2 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sun, 6 Oct 2019 19:47:01 -0700 Subject: [PATCH] some style fixes + solution 004 --- rust/Cargo.lock | 21 +++++++++++++++++++++ rust/Cargo.toml | 1 + rust/src/euler/mod.rs | 3 ++- rust/src/euler/prime.rs | 4 ++-- rust/src/euler/problem_001.rs | 6 +++--- rust/src/euler/problem_002.rs | 8 ++++---- rust/src/euler/problem_003.rs | 4 ++-- rust/src/euler/problem_004.rs | 22 ++++++++++++++++++++++ rust/src/lib.rs | 2 ++ 9 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 rust/src/euler/problem_004.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index aa1924c..8dee346 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1,4 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "either" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "euler-rust" version = "0.1.0" +dependencies = [ + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] +[[package]] +name = "itertools" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +"checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 014ac82..b283821 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" authors = ["evan hemsley "] [dependencies] +itertools = "0.8" \ No newline at end of file diff --git a/rust/src/euler/mod.rs b/rust/src/euler/mod.rs index 43bdc02..1dd8e64 100644 --- a/rust/src/euler/mod.rs +++ b/rust/src/euler/mod.rs @@ -2,4 +2,5 @@ pub mod prime; pub mod problem_001; pub mod problem_002; -pub mod problem_003; \ No newline at end of file +pub mod problem_003; +pub mod problem_004; \ No newline at end of file diff --git a/rust/src/euler/prime.rs b/rust/src/euler/prime.rs index b2aeabc..c5f41a2 100644 --- a/rust/src/euler/prime.rs +++ b/rust/src/euler/prime.rs @@ -11,7 +11,7 @@ fn subfactors(mut n: u64, p: u64) -> (u64, Vec) { r = n % p; } - return (n, subfactors); + (n, subfactors) } pub fn trial_factorization(mut n: u64) -> Vec { @@ -39,7 +39,7 @@ pub fn trial_factorization(mut n: u64) -> Vec { p += 4; } if n >= 1 { factors.push(n); } - return factors; + factors } #[cfg(test)] diff --git a/rust/src/euler/problem_001.rs b/rust/src/euler/problem_001.rs index 127fe03..0b77170 100644 --- a/rust/src/euler/problem_001.rs +++ b/rust/src/euler/problem_001.rs @@ -1,5 +1,5 @@ -fn solution() -> i32 { - return (1..1000).filter( |n| (n % 3 == 0) || (n % 5 == 0)).sum(); +pub fn solution() -> i32 { + (1..1000).filter( |n| (n % 3 == 0) || (n % 5 == 0)).sum() } #[cfg(test)] @@ -8,6 +8,6 @@ mod tests { #[test] fn problem_001() { - assert_eq!(solution(), 233168); + assert_eq!(solution(), 233_168); } } diff --git a/rust/src/euler/problem_002.rs b/rust/src/euler/problem_002.rs index 9e65ef2..5e12d53 100644 --- a/rust/src/euler/problem_002.rs +++ b/rust/src/euler/problem_002.rs @@ -21,11 +21,11 @@ fn fibonacci() -> Fibonacci { } fn fibonacci_sum_up_to(number: u32) -> u32 { - return fibonacci().take_while(|n| n < &number).filter(|n| n % 2 == 0).sum(); + fibonacci().take_while(|n| *n < number).filter(|n| n % 2 == 0).sum() } -fn solution() -> u32 { - return fibonacci_sum_up_to(4000000); +pub fn solution() -> u32 { + fibonacci_sum_up_to(4_000_000) } #[cfg(test)] @@ -34,6 +34,6 @@ mod tests { #[test] fn problem_002() { - assert_eq!(solution(), 4613732); + assert_eq!(solution(), 4_613_732); } } \ No newline at end of file diff --git a/rust/src/euler/problem_003.rs b/rust/src/euler/problem_003.rs index e259782..7a793a9 100644 --- a/rust/src/euler/problem_003.rs +++ b/rust/src/euler/problem_003.rs @@ -1,7 +1,7 @@ use euler::prime; -fn solution() -> Option { - return prime::trial_factorization(600851475143).last().cloned(); +pub fn solution() -> Option { + prime::trial_factorization(600_851_475_143).last().cloned() } #[cfg(test)] diff --git a/rust/src/euler/problem_004.rs b/rust/src/euler/problem_004.rs new file mode 100644 index 0000000..66cfc46 --- /dev/null +++ b/rust/src/euler/problem_004.rs @@ -0,0 +1,22 @@ +use itertools::Itertools; + +fn palindrome(n: u64) -> bool { + let num_string = n.to_string(); + let half = num_string.len() / 2; + + num_string.bytes().take(half).eq(num_string.bytes().rev().take(half)) +} + +pub fn solution() -> Option { + (100..1000).tuple_combinations().map( |(a, b)| a * b ).filter( |n| palindrome(*n)).max() +} + +#[cfg(test)] +mod tests { + use euler::problem_004::solution; + + #[test] + fn problem_004() { + assert_eq!(solution(), Some(906_609)); + } +} \ No newline at end of file diff --git a/rust/src/lib.rs b/rust/src/lib.rs index f20075a..d28fbf9 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1 +1,3 @@ +extern crate itertools; + pub mod euler;