From 43d5faa57eee882b5990d28d76f4ef4aefd9d939 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Tue, 5 Dec 2023 08:11:50 +0000 Subject: [PATCH] 035 ok --- 035/main.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 035/main.rs diff --git a/035/main.rs b/035/main.rs new file mode 100644 index 0000000..689a0c2 --- /dev/null +++ b/035/main.rs @@ -0,0 +1,82 @@ +use std::collections::HashSet; + +fn er() -> HashSet { + let mut nums = Vec::new(); + let size = 1_000_000; + for n in 0..size+1 { nums.push(true); } + let mut p = 1; + while true { + 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 rt(n: u32) -> u32 { + let s = n.to_string(); + let mut chars = s.chars(); + let f = chars.next().unwrap(); + //chars.next_back(); + let mut res = String::from(chars.as_str()); + res.push(f); + res.parse::().unwrap() +} + +fn rts(n: u32) -> Vec { + let mut k = n; + let mut res = Vec::new(); + for _ in 0..n.to_string().len() { + res.push(k); + k = rt(k); + } + res +} + +fn contains_zero(n: u32) -> bool { + let mut a = n; + while a > 0 { + if a % 10 == 0 { + return true; + } + a /= 10; + } + return false; +} + +fn main() { + let ps = er(); + let mut count = 0; + for p in ps.iter() { + if contains_zero(*p) { + continue; + } + let ss = rts(*p); + let mut found_all = true; + for d in ss { + if !ps.contains(&d) { + found_all = false; + break; + } + } + if found_all { + println!("{}", p); + count += 1; + } + } + + println!("count: {}", count); +}