updated prime paths

This commit is contained in:
William Ball 2020-08-03 16:55:20 -07:00
parent 1783bc18bf
commit 38dbb97d7b
4 changed files with 2442 additions and 41 deletions

View file

@ -7,5 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
uint = "0.8.3"
is_prime = "1.0.5"
rand = "0.7.3"

View file

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

File diff suppressed because it is too large Load diff

View file

@ -1,62 +1,70 @@
use std::io::prelude::*;
use uint::construct_uint;
use is_prime::is_prime_with_witnesses;
use rand::prelude::*;
use std::io::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> {
fn step(x: String, rng: &mut ThreadRng) -> Option<String> {
let mut count = 0;
loop {
let mut str_x = format!("{}", x);
count += 1;
if count > 10 * str_x.len() {
if count > 10 * x.len() {
return None;
}
let i = rng.gen_range(0, str_x.len() + 1);
let i = rng.gen_range(0, 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);
let mut copy = x.clone();
copy.insert(i, (d + '0' as u8) as char);
if copy.as_bytes()[0] != '0' as u8 {
if is_prime_with_witnesses(&copy, 40) {
return Some(copy);
}
}
}
}
fn main() {
let mut x = U256::from_dec_str("0").unwrap();
let mut rng = thread_rng();
let mut length = 0;
let mut x;
match rng.gen_range(0, 4) {
0 => x = String::from("2"),
1 => x = String::from("3"),
2 => x = String::from("5"),
3 => x = String::from("7"),
_ => x = String::from("0"),
}
let mut length = 1;
println!("{}", x);
loop {
match step(&x, &mut rng) {
match step(x, &mut rng) {
Some(val) => {
length += 1;
print!("{}, ", x);
std::io::stdout().flush().ok().expect("Could not flush stdout");
println!("{}", val);
std::io::stdout()
.flush()
.ok()
.expect("Could not flush stdout");
x = val;
},
if length > 100 {
length = 1;
match rng.gen_range(0, 4) {
0 => x = String::from("2"),
1 => x = String::from("3"),
2 => x = String::from("5"),
3 => x = String::from("7"),
_ => x = String::from("0"),
}
println!("{}", x);
}
}
None => {
x = U256::from_dec_str("0").unwrap();
println!(" {}\n\n", length);
println!("\n\nUnsucessful! Only length: {}\n\n\n", length);
length = 1;
match rng.gen_range(0, 4) {
0 => x = String::from("2"),
1 => x = String::from("3"),
2 => x = String::from("5"),
3 => x = String::from("7"),
_ => x = String::from("0"),
}
}
}
}