diff --git a/Cargo.lock b/Cargo.lock index 1898679..0d5d3cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,8 @@ +[[package]] +name = "libc" +version = "0.2.50" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "num-bigint" version = "0.2.2" @@ -20,14 +25,25 @@ name = "num-traits" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "num_cpus" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "per" version = "0.1.0" dependencies = [ "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] +"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1" "checksum num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "57450397855d951f1a41305e54851b1a7b8f5d2e349543a02a2effe25459f718" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" +"checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba" diff --git a/Cargo.toml b/Cargo.toml index 01eb5e2..95f7fd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ authors = ["Daniel Løvbrøtte Olsen "] edition = "2018" [dependencies] -num-bigint = "0.2" \ No newline at end of file +num-bigint = "0.2" +num_cpus = "1.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3ca803f..3750167 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 { - 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 { - 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 = string.chars().map(|x| x as u8 - 48).collect(); - numbers.into_iter().product::() + barrier.wait(); + return; }