threading
This commit is contained in:
parent
937c63a985
commit
19f7c49e27
|
@ -1,3 +1,8 @@
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.50"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-bigint"
|
name = "num-bigint"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
@ -20,14 +25,25 @@ name = "num-traits"
|
||||||
version = "0.2.6"
|
version = "0.2.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
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]]
|
[[package]]
|
||||||
name = "per"
|
name = "per"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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]
|
[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-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-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-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"
|
||||||
|
|
|
@ -6,3 +6,4 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num-bigint = "0.2"
|
num-bigint = "0.2"
|
||||||
|
num_cpus = "1.0"
|
122
src/main.rs
122
src/main.rs
|
@ -1,18 +1,51 @@
|
||||||
extern crate num_bigint;
|
extern crate num_bigint;
|
||||||
// extern crate num_traits;
|
// extern crate num_traits;
|
||||||
|
extern crate num_cpus;
|
||||||
|
|
||||||
|
|
||||||
use num_bigint::BigUint;
|
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 {
|
struct Counter {
|
||||||
length: u64,
|
count: BigUint,
|
||||||
prefix: u8,
|
step: u8,
|
||||||
last: BigUint,
|
offset: u8
|
||||||
total: BigUint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Counter {
|
impl Counter {
|
||||||
fn new() -> Counter {
|
fn new(_step: u8, _offset: u8) -> Counter {
|
||||||
Counter {length: 1, prefix: 0, last: BigUint::from(0u8), total: BigUint::from(0u8)}
|
Counter {count: BigUint::from(0u8), step: _step, offset: _offset}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,68 +53,37 @@ impl Iterator for Counter {
|
||||||
type Item = BigUint;
|
type Item = BigUint;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<BigUint> {
|
fn next(&mut self) -> Option<BigUint> {
|
||||||
let old = self.last;
|
// Increment our count. This is why we started at zero.
|
||||||
let old_str = old.to_str_radix(10);
|
self.count += self.step;
|
||||||
let mut new_str = old_str;
|
Some(self.count.clone())
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut num = BigUint::from(0 as u8);
|
let cpus: u8 = num_cpus::get() as u8;
|
||||||
// num = num + 277777788888899u128;
|
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;
|
let mut max: u8 = 0;
|
||||||
|
for x in counter {
|
||||||
let mut n = test(num.clone());
|
let n = x.test();
|
||||||
while n < 10 {
|
if n > max {
|
||||||
n = test(num.clone());
|
|
||||||
if (n > max) {
|
|
||||||
max = n;
|
max = n;
|
||||||
println!("{}: {}", n, num);
|
println!("{} - {}: {}", thread, n, x);
|
||||||
}
|
if n == 10 {
|
||||||
num += 1u8;
|
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;
|
});
|
||||||
}
|
}
|
||||||
|
barrier.wait();
|
||||||
fn factorize(input: BigUint) -> BigUint {
|
return;
|
||||||
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>()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue