finish 34

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-19 19:25:00 +02:00
parent fdb0e04315
commit f70f6f152c
2 changed files with 53 additions and 0 deletions
+8
View File
@@ -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]
+45
View File
@@ -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::<u64>()
== i
{
result.push(i);
};
}
println!("{}", result.iter().sum::<u64>())
}
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<u64>;
}
impl ToDigits for u64 {
fn to_digits(&self) -> Vec<u64> {
self.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as u64)
.collect()
}
}