looking for long sequence

This commit is contained in:
William Ball 2020-07-29 22:41:01 -07:00
parent d1c85455e6
commit bfa4c0ab47
3 changed files with 75 additions and 0 deletions

View file

@ -0,0 +1,11 @@
[package]
name = "insert_digit_anywhere_primes_constant"
version = "0.1.0"
authors = ["William Ball <wball1@swarthmore.edu>"]
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"

View file

@ -0,0 +1 @@
0, 7, 17, 137, 1637, 18637, 198637, 1986037, 19986037, 199860337, 1998660337, 19998660337, 199098660337, 1949098660337, 19490986560337, 194909865603317, 1949098656033817, 19490983656033817,

View file

@ -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<U256> {
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);
}
}
}
}