some style fixes + solution 004

master
Evan Hemsley 2019-10-06 19:47:01 -07:00
parent fa570c3b3e
commit c1dfb112db
9 changed files with 59 additions and 12 deletions

21
rust/Cargo.lock generated
View File

@ -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"

View File

@ -4,3 +4,4 @@ version = "0.1.0"
authors = ["evan hemsley <evan.hemsley@gmail.com>"]
[dependencies]
itertools = "0.8"

View File

@ -2,4 +2,5 @@ pub mod prime;
pub mod problem_001;
pub mod problem_002;
pub mod problem_003;
pub mod problem_003;
pub mod problem_004;

View File

@ -11,7 +11,7 @@ fn subfactors(mut n: u64, p: u64) -> (u64, Vec<u64>) {
r = n % p;
}
return (n, subfactors);
(n, subfactors)
}
pub fn trial_factorization(mut n: u64) -> Vec<u64> {
@ -39,7 +39,7 @@ pub fn trial_factorization(mut n: u64) -> Vec<u64> {
p += 4;
}
if n >= 1 { factors.push(n); }
return factors;
factors
}
#[cfg(test)]

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,7 +1,7 @@
use euler::prime;
fn solution() -> Option<u64> {
return prime::trial_factorization(600851475143).last().cloned();
pub fn solution() -> Option<u64> {
prime::trial_factorization(600_851_475_143).last().cloned()
}
#[cfg(test)]

View File

@ -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<u64> {
(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));
}
}

View File

@ -1 +1,3 @@
extern crate itertools;
pub mod euler;