You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4 KiB

11 months ago
use std::collections::HashSet;
fn er() -> HashSet<u32> {
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::<u32>().unwrap()
}
fn test(p: u32, hs: &HashSet<u32>) -> 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);
}