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}; use std::time::{Duration, Instant}; 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 = string.chars().map(|x| x as u8 - 48).collect(); numbers.into_iter().product::() } 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 { count: BigUint, step: u8, offset: u8 } impl Counter { fn new(_step: u8, _offset: u8) -> Counter { Counter {count: BigUint::from(0u8), step: _step, offset: _offset} } } impl Iterator for Counter { type Item = BigUint; fn next(&mut self) -> Option { // Increment our count. This is why we started at zero. self.count += self.step; Some(self.count.clone()) } } fn main() { let start = Instant::now(); let cpus: u8 = num_cpus::get() as u8; println!("{:?}", cpus); 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; } } } }); } barrier.wait(); println!("Finding n = 10 took {:#?}", start.elapsed()); return; }