diff --git a/rust/insert_digit_anywhere_primes_ev/Cargo.toml b/rust/insert_digit_anywhere_primes_ev/Cargo.toml new file mode 100644 index 0000000..317126f --- /dev/null +++ b/rust/insert_digit_anywhere_primes_ev/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "insert_digit_anywhere_primes_constant" +version = "0.1.0" +authors = ["William Ball "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +uint = "0.8.3" +rand = "0.7.3" diff --git a/rust/insert_digit_anywhere_primes_ev/log b/rust/insert_digit_anywhere_primes_ev/log new file mode 100644 index 0000000..1d60a08 --- /dev/null +++ b/rust/insert_digit_anywhere_primes_ev/log @@ -0,0 +1 @@ +0, 7, 17, 137, 1637, 18637, 198637, 1986037, 19986037, 199860337, 1998660337, 19998660337, 199098660337, 1949098660337, 19490986560337, 194909865603317, 1949098656033817, 19490983656033817, \ No newline at end of file diff --git a/rust/insert_digit_anywhere_primes_ev/src/main.rs b/rust/insert_digit_anywhere_primes_ev/src/main.rs new file mode 100644 index 0000000..5db0cb4 --- /dev/null +++ b/rust/insert_digit_anywhere_primes_ev/src/main.rs @@ -0,0 +1,63 @@ +use std::io::prelude::*; +use uint::construct_uint; +use rand::prelude::*; + +construct_uint! { + pub struct U256(4); +} + +fn is_prime(x: &U256) -> bool { + let zero = U256::from_dec_str("0").unwrap(); + let six = U256::from_dec_str("6").unwrap(); + if x % 2 == zero || x % 3 == zero { + return false; + } + let mut i = U256::from_dec_str("5").unwrap(); + while i * i < *x { + if x % i == zero || x % (i + 2) == zero { + return false; + } + i += six; + } + true +} + +fn step(x: &U256, rng: &mut ThreadRng) -> Option { + let mut count = 0; + loop { + let mut str_x = format!("{}", x); + count += 1; + if count > 10 * str_x.len() { + return None; + } + let i = rng.gen_range(0, str_x.len() + 1); + let d = rng.gen_range(0, 10) as u8; + str_x.insert(i, (d + '0' as u8) as char); + let temp = U256::from_dec_str(&str_x).unwrap(); + if !(i == 0 && d == 0) { + if is_prime(&temp) { + return Some(temp); + } + } + } +} + +fn main() { + let mut x = U256::from_dec_str("0").unwrap(); + let mut rng = thread_rng(); + let mut length = 0; + loop { + match step(&x, &mut rng) { + Some(val) => { + length += 1; + print!("{}, ", x); + std::io::stdout().flush().ok().expect("Could not flush stdout"); + x = val; + }, + None => { + x = U256::from_dec_str("0").unwrap(); + println!(" {}\n\n", length); + } + } + } +}