add flags
This commit is contained in:
69
src/main.rs
69
src/main.rs
@@ -1,34 +1,39 @@
|
||||
extern crate num_bigint;
|
||||
// extern crate num_traits;
|
||||
extern crate num_cpus;
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
|
||||
|
||||
|
||||
use clap::App;
|
||||
|
||||
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;
|
||||
|
||||
trait Peristance {
|
||||
fn per_mul(&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();
|
||||
impl Peristance for BigUint {
|
||||
fn per_mul(&self) -> u8 {
|
||||
let mut n = self.to_radix_le(10)
|
||||
.into_iter()
|
||||
.product::<BigUint>();
|
||||
|
||||
let mut counter = 1;
|
||||
while n.to_str_radix(10).chars().count() > 1 {
|
||||
n = n.factorize();
|
||||
n = n.to_radix_le(10)
|
||||
.into_iter()
|
||||
.product::<BigUint>();
|
||||
counter += 1;
|
||||
}
|
||||
return counter;
|
||||
@@ -61,23 +66,40 @@ impl Iterator for Counter {
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
let cpus: u8 = num_cpus::get() as u8;
|
||||
println!("{:?}", cpus);
|
||||
|
||||
|
||||
let args = App::new("Persistance")
|
||||
.version("0.2.0")
|
||||
.author("Daniel Olsen")
|
||||
.about("Finds the sequence of smallest number with a multiplicative persistance of n")
|
||||
.args_from_usage("-j, --jobs=[threads] 'Sets the number of worker threads to spin up, if 0 or empty this equals the number of cores on your system (INCLUDING HYPERTHREADS)'
|
||||
-m, --max=[max] 'Sets the maximum n you want to calculate'")
|
||||
.get_matches();
|
||||
|
||||
|
||||
let mut cpus = value_t!(args, "jobs", u8).unwrap_or(num_cpus::get() as u8);
|
||||
if cpus == 0 {
|
||||
cpus = num_cpus::get() as u8;
|
||||
}
|
||||
let cpus = cpus;
|
||||
|
||||
let max = value_t!(args, "max", u8).unwrap_or(11u8);
|
||||
|
||||
println!("threads: {}", 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 top: u8 = 0;
|
||||
for x in counter {
|
||||
let n = x.test();
|
||||
if n > max {
|
||||
max = n;
|
||||
println!("{} - {}: {}", thread, n, x);
|
||||
if n == 10 {
|
||||
let n = x.per_mul();
|
||||
if n > top {
|
||||
top = n;
|
||||
println!("{:?} {} - {}: {}", start.elapsed(), thread, n, x);
|
||||
if n > max-1 {
|
||||
c.wait();
|
||||
return;
|
||||
}
|
||||
@@ -86,6 +108,7 @@ fn main() {
|
||||
});
|
||||
}
|
||||
barrier.wait();
|
||||
println!("Finding n = 10 took {:#?}", start.elapsed());
|
||||
|
||||
println!("Finding took {:#?}", start.elapsed());
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user