From f70f6f152c4cc81feb2e15f58a7e88064939942a Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Fri, 19 Aug 2022 19:25:00 +0200 Subject: [PATCH] finish 34 --- src/euler34/Cargo.toml | 8 ++++++++ src/euler34/src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/euler34/Cargo.toml create mode 100644 src/euler34/src/main.rs diff --git a/src/euler34/Cargo.toml b/src/euler34/Cargo.toml new file mode 100644 index 0000000..05c7f71 --- /dev/null +++ b/src/euler34/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "euler34" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/euler34/src/main.rs b/src/euler34/src/main.rs new file mode 100644 index 0000000..54bd710 --- /dev/null +++ b/src/euler34/src/main.rs @@ -0,0 +1,45 @@ +use std::collections::HashMap; + +fn main() { + let mut hm = HashMap::new(); + for i in 0..10 { + hm.insert(i, factorial(i)); + } + + let mut result = vec![]; + + for i in 3..1_000_000_u64 { + if i.to_digits() + .iter() + .map(|d| hm.get(d).unwrap()) + .sum::() + == i + { + result.push(i); + }; + } + println!("{}", result.iter().sum::()) +} + +fn factorial(x: u64) -> u64 { + if x == 0 { + 1 + } else if x == 1 { + x + } else { + x * factorial(x - 1) + } +} + +trait ToDigits { + fn to_digits(&self) -> Vec; +} + +impl ToDigits for u64 { + fn to_digits(&self) -> Vec { + self.to_string() + .chars() + .map(|c| c.to_digit(10).unwrap() as u64) + .collect() + } +}