From 2657e336c2c0db79e2a97576b586cc18170cceb1 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 16 Dec 2023 13:33:25 +0000 Subject: [PATCH] 041 ok --- 041/main.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 041/main.rs diff --git a/041/main.rs b/041/main.rs new file mode 100644 index 0000000..15f4fd2 --- /dev/null +++ b/041/main.rs @@ -0,0 +1,89 @@ +use std::collections::HashSet; +use std::collections::VecDeque; + +type Str = VecDeque; +type Res = HashSet; + +fn pr(s: &Str) { + for c in s.iter() { print!("{}", c); } + println!(""); +} + +fn is_prime(k: u32) -> bool { + if k < 0 { + return false; + } + for i in 2..k { + if k % i == 0 { + return false; + } + } + return true; +} + +fn nstr(n: u32) -> Str { + let mut res = Str::new(); + let mut nn = n; + while nn > 0 { + res.push_front((nn % 10) as u8); + nn /= 10; + } + res +} + + +fn strn(s: &Str) -> u32 { + let mut res = 0; + for d in s.iter() { + res *= 10; + res += *d as u32; + } + res +} + + +fn test(s: &Str, hs: &mut Res) { + let n = strn(s); + if is_prime(n) { + //println!("{}", n); + hs.insert(n); + } +} + +fn sp(s: &Str, fx: usize, k: usize) -> Str { + let mut res = Str::new(); + for i in 0..fx { + res.push_back(s[i]); + } + res.push_back(s[k]); + for i in fx..s.len() { + if i != k { res.push_back(s[i]); } + } + res +} + +fn lp(s: &Str, fx: usize, keep_first: bool, hs: &mut Res) { + let kf = 0; //if keep_first { 0usize } else { 1usize }; + for k in (fx + kf)..s.len() { + let ss = sp(&s, fx, k); + //count += 1; + if ss != *s || keep_first { + test(&ss, hs); + } + lp(&ss, fx + 1, false, hs); + } +} + +fn main() { + let mut hs = HashSet::new(); + let mut s = nstr(1234567); // manual test of length + let mut max = 0; + let m = lp(&s, 0, true, &mut hs); + for h in hs.iter() { + if *h > max { + max = *h; + } + } + println!("{}", max); +} +