2019-03-22 15:47:19 +01:00
|
|
|
extern crate num_bigint;
|
|
|
|
// extern crate num_traits;
|
2019-03-25 11:02:52 +01:00
|
|
|
extern crate num_cpus;
|
|
|
|
|
2019-03-22 15:47:19 +01:00
|
|
|
|
|
|
|
use num_bigint::BigUint;
|
|
|
|
|
2019-03-25 11:02:52 +01:00
|
|
|
use std::thread;
|
|
|
|
// use std::sync::mpsc;
|
|
|
|
use std::sync::{Arc, Barrier, RwLock};
|
|
|
|
|
2019-03-25 23:20:15 +01:00
|
|
|
use std::time::{Duration, Instant};
|
2019-03-25 11:02:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
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)]
|
2019-03-24 18:07:14 +01:00
|
|
|
struct Counter {
|
2019-03-25 11:02:52 +01:00
|
|
|
count: BigUint,
|
|
|
|
step: u8,
|
|
|
|
offset: u8
|
2019-03-24 18:07:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Counter {
|
2019-03-25 11:02:52 +01:00
|
|
|
fn new(_step: u8, _offset: u8) -> Counter {
|
|
|
|
Counter {count: BigUint::from(0u8), step: _step, offset: _offset}
|
2019-03-24 18:07:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for Counter {
|
|
|
|
type Item = BigUint;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<BigUint> {
|
2019-03-25 11:02:52 +01:00
|
|
|
// Increment our count. This is why we started at zero.
|
|
|
|
self.count += self.step;
|
|
|
|
Some(self.count.clone())
|
2019-03-24 18:07:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-03-25 23:20:15 +01:00
|
|
|
let start = Instant::now();
|
2019-03-25 11:02:52 +01:00
|
|
|
let cpus: u8 = num_cpus::get() as u8;
|
|
|
|
println!("{:?}", cpus);
|
2019-03-22 15:47:19 +01:00
|
|
|
|
2019-03-25 11:02:52 +01:00
|
|
|
let barrier = Arc::new(Barrier::new(2));
|
2019-03-22 15:47:19 +01:00
|
|
|
|
2019-03-25 11:02:52 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
barrier.wait();
|
2019-03-25 23:20:15 +01:00
|
|
|
println!("Finding n = 10 took {:#?}", start.elapsed());
|
2019-03-25 11:02:52 +01:00
|
|
|
return;
|
2019-03-24 18:07:14 +01:00
|
|
|
}
|