From d8a8e58e5ec6bceea58b54574e280cabb2c4d0e3 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 9 Dec 2023 09:24:36 +0000 Subject: [PATCH] 039 ok --- 039/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 039/main.rs diff --git a/039/main.rs b/039/main.rs new file mode 100644 index 0000000..8fab382 --- /dev/null +++ b/039/main.rs @@ -0,0 +1,39 @@ +use std::collections::HashMap; +const SIZE: u32 = 1000u32; + +fn squares() -> HashMap { + let mut res = HashMap::new(); + for n in 1..=SIZE { + res.insert(n*n, n); + } + res +} +fn main() { + let mut sq = squares(); + let mut ps = HashMap::new(); + for a in 1..SIZE { + for b in 1..SIZE { + let c2 = a*a + b*b; + if sq.contains_key(&c2) { + let c = *sq.get(&c2).unwrap(); + let p = a + b + c; + if p > SIZE { continue; } + if !ps.contains_key(&p) { + ps.insert(p, 1); + } else { + let count = ps.get(&p).unwrap(); + ps.insert(p, count + 1); + } + } + } + } + let mut max_v = 0u32; + let mut max_k = 0u32; + for (k, v) in ps.iter() { + if *v > max_v { + max_v = *v; + max_k = *k; + } + } + println!("{}", max_k); +}