From 32ae71cf32f7b86e06fa8dc68962b1cea7c2d3db Mon Sep 17 00:00:00 2001 From: evan hemsley Date: Thu, 20 Sep 2018 15:11:45 -0700 Subject: [PATCH] rust solutions problems 2 and 3 --- rust/src/euler/mod.rs | 4 +++ rust/src/euler/prime.rs | 53 +++++++++++++++++++++++++++++++++++ rust/src/euler/problem_002.rs | 39 ++++++++++++++++++++++++++ rust/src/euler/problem_003.rs | 15 ++++++++++ 4 files changed, 111 insertions(+) create mode 100644 rust/src/euler/prime.rs create mode 100644 rust/src/euler/problem_002.rs create mode 100644 rust/src/euler/problem_003.rs diff --git a/rust/src/euler/mod.rs b/rust/src/euler/mod.rs index c5d6720..43bdc02 100644 --- a/rust/src/euler/mod.rs +++ b/rust/src/euler/mod.rs @@ -1 +1,5 @@ +pub mod prime; + pub mod problem_001; +pub mod problem_002; +pub mod problem_003; \ No newline at end of file diff --git a/rust/src/euler/prime.rs b/rust/src/euler/prime.rs new file mode 100644 index 0000000..b2aeabc --- /dev/null +++ b/rust/src/euler/prime.rs @@ -0,0 +1,53 @@ +fn subfactors(mut n: u64, p: u64) -> (u64, Vec) { + let mut subfactors = Vec::new(); + + let mut q = n / p; + let mut r = n % p; + + while r == 0 { + subfactors.push(p); + n = q; + q = n / p; + r = n % p; + } + + return (n, subfactors); +} + +pub fn trial_factorization(mut n: u64) -> Vec { + let mut factors = Vec::new(); + + let (num, division_by_two) = subfactors(n, 2); + n = num; // is there a clean way to avoid this assignment? + factors.extend(division_by_two); + + let (num, division_by_three) = subfactors(n, 3); + n = num; + factors.extend(division_by_three); + + let mut p = 5; + while p * p <= n + { + let (num, division_by_p) = subfactors(n, p); + n = num; + factors.extend(division_by_p); + p += 2; + + let (num, division_by_p_plus_two) = subfactors(n, p); + n = num; + factors.extend(division_by_p_plus_two); + p += 4; + } + if n >= 1 { factors.push(n); } + return factors; +} + +#[cfg(test)] +mod tests { + use euler::prime::trial_factorization; + + #[test] + fn test() { + assert_eq!(trial_factorization(20).len(), 3); + } +} diff --git a/rust/src/euler/problem_002.rs b/rust/src/euler/problem_002.rs new file mode 100644 index 0000000..9e65ef2 --- /dev/null +++ b/rust/src/euler/problem_002.rs @@ -0,0 +1,39 @@ +struct Fibonacci { + curr: u32, + next: u32, +} + +impl Iterator for Fibonacci { + type Item = u32; + + fn next(&mut self) -> Option { + let new = self.curr + self.next; + + self.curr = self.next; + self.next = new; + + Some(self.curr) + } +} + +fn fibonacci() -> Fibonacci { + Fibonacci { curr: 1, next: 1 } +} + +fn fibonacci_sum_up_to(number: u32) -> u32 { + return fibonacci().take_while(|n| n < &number).filter(|n| n % 2 == 0).sum(); +} + +fn solution() -> u32 { + return fibonacci_sum_up_to(4000000); +} + +#[cfg(test)] +mod tests { + use euler::problem_002::solution; + + #[test] + fn problem_002() { + assert_eq!(solution(), 4613732); + } +} \ No newline at end of file diff --git a/rust/src/euler/problem_003.rs b/rust/src/euler/problem_003.rs new file mode 100644 index 0000000..e259782 --- /dev/null +++ b/rust/src/euler/problem_003.rs @@ -0,0 +1,15 @@ +use euler::prime; + +fn solution() -> Option { + return prime::trial_factorization(600851475143).last().cloned(); +} + +#[cfg(test)] +mod tests { + use euler::problem_003::solution; + + #[test] + fn problem_003() { + assert_eq!(solution().unwrap(), 6857); + } +} \ No newline at end of file