finish 30

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-19 16:29:40 +02:00
parent 0b7e604056
commit 5470995dc2
2 changed files with 32 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "euler30"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+24
View File
@@ -0,0 +1,24 @@
use std::collections::HashMap;
fn main() {
let hm: HashMap<_, _> = (0..10_u64).into_iter().map(|d| (d, d.pow(5))).collect();
let result: Vec<_> = (2..=10_000_000)
.into_iter()
.filter(|n| is_valid(*n, &hm))
.collect();
println!("{}", result.iter().sum::<u64>());
}
fn sum_of_power_of_digits(x: u64, hm: &HashMap<u64, u64>) -> u64 {
x.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as u64)
.map(|d| hm.get(&d).unwrap())
.sum()
}
fn is_valid(x: u64, hm: &HashMap<u64, u64>) -> bool {
x == sum_of_power_of_digits(x, hm)
}