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.
83 lines
1.7 KiB
83 lines
1.7 KiB
1 year 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 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::<u32>().unwrap()
|
||
|
}
|
||
|
|
||
|
fn rts(n: u32) -> Vec<u32> {
|
||
|
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);
|
||
|
}
|