diff --git a/rust/fourthfree/Cargo.toml b/rust/fourthfree/Cargo.toml new file mode 100644 index 0000000..ed6ff3b --- /dev/null +++ b/rust/fourthfree/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "fourthfree" +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] +rug = "1.10.0" diff --git a/rust/fourthfree/src/main.rs b/rust/fourthfree/src/main.rs new file mode 100644 index 0000000..30790ac --- /dev/null +++ b/rust/fourthfree/src/main.rs @@ -0,0 +1,79 @@ +use rug::{Assign, Integer}; +use std::thread; + +const NUM_THREADS: usize = 12; +const BASE: u64 = 10; + +fn is_fourth_free(x: &Integer) -> bool { + let mut i = Integer::new(); + i.assign(2); + let mut fourth = Integer::from(&Integer::from(&i * &i) * &Integer::from(&i * &i)); + while fourth < *x { + if x % (fourth) == 0 { + return false; + } + i += 1; + fourth = Integer::from(&Integer::from(&i * &i) * &Integer::from(&i * &i)); + } + true +} + +fn step(x: &Integer) -> Vec { + let mut new_xs = Vec::new(); + for d in 0..BASE { + let mut temp = Integer::from(x * BASE); + temp += d; + if is_fourth_free(&temp) { + new_xs.push(temp); + } + } + new_xs +} + +fn next(ls: &Vec) -> Vec { + let mut new: Vec = Vec::new(); + let mut slices: Vec> = Vec::new(); + let mut children = Vec::with_capacity(NUM_THREADS); + let size = ls.len() / NUM_THREADS; + for i in 0..(NUM_THREADS - 1) { + let mut new = Vec::with_capacity(size); + for val in ls.iter().skip(size * i).take(size) { + new.push(val.clone()); + } + slices.push(new); + } + { + let mut new = Vec::new(); + for val in ls.iter().skip(size * (NUM_THREADS - 1)) { + new.push(val.clone()); + } + slices.push(new); + } + for slice in slices { + children.push(thread::spawn(move || -> Vec { + let mut new = Vec::new(); + for oldval in slice { + new.append(&mut step(&oldval)); + } + new + })); + } + for child in children { + new.append(&mut child.join().unwrap()); + } + new +} + +fn main() { + let mut i = 0; + let mut ls: Vec = (1..BASE) + .into_iter() + .map(|x| Integer::from(x)) + .filter(|x| is_fourth_free(x)) + .collect(); + loop { + i += 1; + println!("{}\t{}", i, ls.len()); + ls = next(&ls); + } +} diff --git a/rust/insert_digit_anywhere_primes_constant/log b/rust/insert_digit_anywhere_primes_constant/log new file mode 100644 index 0000000..2c2539c --- /dev/null +++ b/rust/insert_digit_anywhere_primes_constant/log @@ -0,0 +1,9 @@ +6 1.3815510557964277 +7.675 1.7672340588729303 +8.657602339181286 1.9934866087269214 +9.287908478277238 2.1386199607174183 +9.706992706401598 2.235117670356225 +10.021722699613848 2.3075869294250895 +10.26388157204211 2.3633460704040457 +10.456005647430802 2.407584273603572 +10.613201775385221 2.4437800196939956 diff --git a/rust/insert_digit_anywhere_primes_constant/src/main.rs b/rust/insert_digit_anywhere_primes_constant/src/main.rs index 5405e5a..a782762 100644 --- a/rust/insert_digit_anywhere_primes_constant/src/main.rs +++ b/rust/insert_digit_anywhere_primes_constant/src/main.rs @@ -2,8 +2,8 @@ use std::thread; use std::collections::HashSet; const NUM_THREADS: usize = 6; -const THREAD_SKIP: u64 = 1_000; -const BASE: u64 = 16; +// const THREAD_SKIP: u64 = 1_000; +const BASE: u64 = 10; fn remove_duplicates(ls: Vec) -> Vec { let mut hs: HashSet = HashSet::new(); @@ -82,11 +82,11 @@ fn next(ls: Vec) -> (f64, Vec) { let mut new = Vec::new(); let mut count = 0; for oldval in slice { - // let mut temp = step(oldval); - let temp = step(oldval); + let mut temp = step(oldval); + // let temp = step(oldval); count += temp.len() as u64; - new.push(oldval + THREAD_SKIP); - // new.append(&mut temp); + // new.push(oldval + THREAD_SKIP); + new.append(&mut temp); } (count, new) })); @@ -100,13 +100,13 @@ fn next(ls: Vec) -> (f64, Vec) { } fn main() { - let mut ls: Vec = (1..(NUM_THREADS as u64) * THREAD_SKIP) - .into_iter() - .collect(); + // let mut ls: Vec = (1..(NUM_THREADS as u64) * THREAD_SKIP) + // .into_iter() + // .collect(); let mut count: f64; let mut i = 0; let mut total: f64 = 0.0; - // let mut ls = primal::Primes::all().take_while(|x| *x < BASE as usize).map(|x| x as u64).collect(); + let mut ls = primal::Primes::all().take_while(|x| *x < BASE as usize).map(|x| x as u64).collect(); loop { let res = next(ls); count = res.0;