threading

This commit is contained in:
Daniel Løvbrøtte Olsen
2019-03-25 11:02:52 +01:00
parent 937c63a985
commit 19f7c49e27
3 changed files with 84 additions and 65 deletions

View File

@@ -1,18 +1,51 @@
extern crate num_bigint;
// extern crate num_traits;
extern crate num_cpus;
use num_bigint::BigUint;
use std::thread;
// use std::sync::mpsc;
use std::sync::{Arc, Barrier, RwLock};
trait Stuff {
fn factorize(&self) -> Self;
fn test(&self) -> u8;
}
impl Stuff for BigUint {
fn factorize(&self) -> BigUint {
let string = self.to_str_radix(10);
let numbers: Vec<u8> = string.chars().map(|x| x as u8 - 48).collect();
numbers.into_iter().product::<BigUint>()
}
fn test(&self) -> u8 {
let mut n = self.factorize();
let mut counter = 1;
while n.to_str_radix(10).chars().count() > 1 {
n = n.factorize();
counter += 1;
}
return counter;
}
}
#[derive(Debug)]
struct Counter {
length: u64,
prefix: u8,
last: BigUint,
total: BigUint
count: BigUint,
step: u8,
offset: u8
}
impl Counter {
fn new() -> Counter {
Counter {length: 1, prefix: 0, last: BigUint::from(0u8), total: BigUint::from(0u8)}
fn new(_step: u8, _offset: u8) -> Counter {
Counter {count: BigUint::from(0u8), step: _step, offset: _offset}
}
}
@@ -20,68 +53,37 @@ impl Iterator for Counter {
type Item = BigUint;
fn next(&mut self) -> Option<BigUint> {
let old = self.last;
let old_str = old.to_str_radix(10);
let mut new_str = old_str;
let mut carry = true;
let mut index = 0;
while carry != true {
match old_str[old_str.chars().count() - 1 - index] {
'5' => {carry = false; new_str[new_str.chars().count() - 1 - index] = '7'},
'7' => {carry = false; new_str[new_str.chars().count() - 1 - index] = '8'},
'8' => {carry = false; new_str[new_str.chars().count() - 1 - index] = '9'},
'9' => {carry = true; index += 1; new_str[new_str.chars().count() - 1 - index + 1] = '5'}
}
}
if carry == true {
self.prefix += 1;
}
let new_string = String::new();
new_string.push_str(match self.prefix {
0 => "",
1 => "2",
2 => "3",
3 => "4",
4 => "6",
5 => "26"
});
new_string.push_str(new_str);
return BigUint::from_str(new_string);
// Increment our count. This is why we started at zero.
self.count += self.step;
Some(self.count.clone())
}
}
fn main() {
let mut num = BigUint::from(0 as u8);
// num = num + 277777788888899u128;
let mut max: u8 = 0;
let cpus: u8 = num_cpus::get() as u8;
println!("{:?}", cpus);
let mut n = test(num.clone());
while n < 10 {
n = test(num.clone());
if (n > max) {
max = n;
println!("{}: {}", n, num);
}
num += 1u8;
let barrier = Arc::new(Barrier::new(2));
for thread in 0..(cpus) {
let c = barrier.clone();
thread::spawn(move || {
println!("Started thread {}!", thread);
let counter = Counter::new(cpus-thread, cpus);
let mut max: u8 = 0;
for x in counter {
let n = x.test();
if n > max {
max = n;
println!("{} - {}: {}", thread, n, x);
if n == 10 {
c.wait();
return;
}
}
}
});
}
}
fn test(input: BigUint) -> u8 {
let mut n = factorize(input);
let mut counter = 1;
while (n.to_str_radix(10).chars().count() > 1) {
n = factorize(n);
// println!("n: {:?}", n);
counter += 1;
}
return counter;
}
fn factorize(input: BigUint) -> BigUint {
let string = input.to_str_radix(10);
let numbers: Vec<u8> = string.chars().map(|x| x as u8 - 48).collect();
numbers.into_iter().product::<BigUint>()
barrier.wait();
return;
}