diff --git a/037/main.rs b/037/main.rs new file mode 100644 index 0000000..ebccf91 --- /dev/null +++ b/037/main.rs @@ -0,0 +1,64 @@ +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 trl(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.parse::().unwrap() +} + +fn test(p: u32, hs: &HashSet) -> bool { + let mut tl = p; + while tl > 10 { + if !hs.contains(&tl) { return false; } + tl = trl(tl); + } + if !hs.contains(&tl) { return false; } + let mut tr = p; + while tr > 0 { + if !hs.contains(&tr) { return false; } + tr /= 10; + } + return true; +} + +fn main() { + let hs = er(); + let mut sum = 0; + for p in hs.iter() { + if *p < 10 { continue; } + if test(*p, &hs) { + println!("{}", p); + sum += p; + } + } + println!("sum: {}", sum); +}