finish 32

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-19 17:10:18 +02:00
parent 5470995dc2
commit c57b74f986
2 changed files with 50 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "euler32"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+42
View File
@@ -0,0 +1,42 @@
use std::collections::HashSet;
type Int = u64;
fn main() {
let f: Vec<_> = (1..10_000_u64)
.filter(|n| {
let s = n.to_string();
let hs = s.chars().collect::<HashSet<_>>();
hs.len() == s.len() && !hs.contains(&'0')
})
.collect();
let mut result = HashSet::new();
for f1 in &f {
for f2 in f.iter().rev() {
let p = f1 * f2;
if are_pandigital([f1, f2, &p]) {
println!("{f1} {f2} {p}");
result.insert(p);
}
}
}
println!("{}", result.iter().sum::<u64>());
}
fn are_pandigital(input: [&Int; 3]) -> bool {
let mut accum = HashSet::new();
let mut length = 0;
for n in input {
let s = n.to_string();
length += s.len();
let hs = s.chars().collect::<HashSet<_>>();
if s.len() != hs.len() {
return false;
}
for item in hs {
accum.insert(item);
}
}
accum.len() == 9 && length == 9 && !accum.contains(&'0')
}