From 2a5359f15ff63e1dc4c5da52fd7147d1037e27c7 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sun, 17 Dec 2023 13:34:41 +0000 Subject: [PATCH] 046 ok --- 046/main.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 046/main.rs diff --git a/046/main.rs b/046/main.rs new file mode 100644 index 0000000..fc5a1e7 --- /dev/null +++ b/046/main.rs @@ -0,0 +1,59 @@ +use std::collections::HashSet; + +type HS = HashSet; + +fn er() -> HS { + let mut nums = Vec::new(); + let size = 1_000_000; + for n in 0..size+1 { nums.push(true); } + let mut p = 1; + loop { + let mut found = false; + for i in p+1..size { + if nums[i] { + found = true; + p = i; + break; + } + } + if !found { break; } + for i in (2 * p .. size).step_by(p) { + nums[i] = false; + } + } + let mut res = HashSet::new(); + for i in 2..size { + if nums[i] { res.insert(i as u32); } + } + return res; +} + +fn sqs() -> HS { + let mut res = HS::new(); + for n in 1..1_000_0 { + res.insert(n*n); + } + return res; +} + +fn test(n: u32, ps: &HS, sq: &HS) -> bool { + for k in 7..n { + if !ps.contains(&k) { continue } + let s = (n - k) / 2; + if !sq.contains(&s) { continue } + return true; + } + return false; +} + +fn main() { + let ps = er(); + let sq = sqs(); + for k in (9..100000).step_by(2) { + if ps.contains(&k) { continue; } + if !test(k, &ps, &sq) { + println!("{}", k); + break; + } + } +}