added fourthfree calculations rust

This commit is contained in:
William Ball 2020-07-24 12:26:20 -07:00
parent a6e62a99cc
commit 2a9682aeab
4 changed files with 108 additions and 10 deletions

View file

@ -0,0 +1,10 @@
[package]
name = "fourthfree"
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]
rug = "1.10.0"

View file

@ -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<Integer> {
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<Integer>) -> Vec<Integer> {
let mut new: Vec<Integer> = Vec::new();
let mut slices: Vec<Vec<Integer>> = 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<Integer> {
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<Integer> = (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);
}
}

View file

@ -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

View file

@ -2,8 +2,8 @@ use std::thread;
use std::collections::HashSet; use std::collections::HashSet;
const NUM_THREADS: usize = 6; const NUM_THREADS: usize = 6;
const THREAD_SKIP: u64 = 1_000; // const THREAD_SKIP: u64 = 1_000;
const BASE: u64 = 16; const BASE: u64 = 10;
fn remove_duplicates(ls: Vec<u64>) -> Vec<u64> { fn remove_duplicates(ls: Vec<u64>) -> Vec<u64> {
let mut hs: HashSet<u64> = HashSet::new(); let mut hs: HashSet<u64> = HashSet::new();
@ -82,11 +82,11 @@ fn next(ls: Vec<u64>) -> (f64, Vec<u64>) {
let mut new = Vec::new(); let mut new = Vec::new();
let mut count = 0; let mut count = 0;
for oldval in slice { for oldval in slice {
// let mut temp = step(oldval); let mut temp = step(oldval);
let temp = step(oldval); // let temp = step(oldval);
count += temp.len() as u64; count += temp.len() as u64;
new.push(oldval + THREAD_SKIP); // new.push(oldval + THREAD_SKIP);
// new.append(&mut temp); new.append(&mut temp);
} }
(count, new) (count, new)
})); }));
@ -100,13 +100,13 @@ fn next(ls: Vec<u64>) -> (f64, Vec<u64>) {
} }
fn main() { fn main() {
let mut ls: Vec<u64> = (1..(NUM_THREADS as u64) * THREAD_SKIP) // let mut ls: Vec<u64> = (1..(NUM_THREADS as u64) * THREAD_SKIP)
.into_iter() // .into_iter()
.collect(); // .collect();
let mut count: f64; let mut count: f64;
let mut i = 0; let mut i = 0;
let mut total: f64 = 0.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 { loop {
let res = next(ls); let res = next(ls);
count = res.0; count = res.0;