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); }