finish 30
This commit is contained in:
@@ -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]
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user