updated prime paths
This commit is contained in:
parent
1783bc18bf
commit
38dbb97d7b
4 changed files with 2442 additions and 41 deletions
|
|
@ -7,5 +7,5 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
uint = "0.8.3"
|
is_prime = "1.0.5"
|
||||||
rand = "0.7.3"
|
rand = "0.7.3"
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
0, 7, 17, 137, 1637, 18637, 198637, 1986037, 19986037, 199860337, 1998660337, 19998660337, 199098660337, 1949098660337, 19490986560337, 194909865603317, 1949098656033817, 19490983656033817,
|
|
||||||
2394
rust/insert_digit_anywhere_primes_ev/prime_paths
Normal file
2394
rust/insert_digit_anywhere_primes_ev/prime_paths
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,62 +1,70 @@
|
||||||
use std::io::prelude::*;
|
use is_prime::is_prime_with_witnesses;
|
||||||
use uint::construct_uint;
|
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
construct_uint! {
|
fn step(x: String, rng: &mut ThreadRng) -> Option<String> {
|
||||||
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;
|
let mut count = 0;
|
||||||
loop {
|
loop {
|
||||||
let mut str_x = format!("{}", x);
|
|
||||||
count += 1;
|
count += 1;
|
||||||
if count > 10 * str_x.len() {
|
if count > 10 * x.len() {
|
||||||
return None;
|
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;
|
let d = rng.gen_range(0, 10) as u8;
|
||||||
str_x.insert(i, (d + '0' as u8) as char);
|
let mut copy = x.clone();
|
||||||
let temp = U256::from_dec_str(&str_x).unwrap();
|
copy.insert(i, (d + '0' as u8) as char);
|
||||||
if !(i == 0 && d == 0) {
|
if copy.as_bytes()[0] != '0' as u8 {
|
||||||
if is_prime(&temp) {
|
if is_prime_with_witnesses(©, 40) {
|
||||||
return Some(temp);
|
return Some(copy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x = U256::from_dec_str("0").unwrap();
|
|
||||||
let mut rng = thread_rng();
|
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 {
|
loop {
|
||||||
match step(&x, &mut rng) {
|
match step(x, &mut rng) {
|
||||||
Some(val) => {
|
Some(val) => {
|
||||||
length += 1;
|
length += 1;
|
||||||
print!("{}, ", x);
|
println!("{}", val);
|
||||||
std::io::stdout().flush().ok().expect("Could not flush stdout");
|
std::io::stdout()
|
||||||
|
.flush()
|
||||||
|
.ok()
|
||||||
|
.expect("Could not flush stdout");
|
||||||
x = val;
|
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 => {
|
None => {
|
||||||
x = U256::from_dec_str("0").unwrap();
|
println!("\n\nUnsucessful! Only length: {}\n\n\n", length);
|
||||||
println!(" {}\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"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue