add flags

This commit is contained in:
Daniel Løvbrøtte Olsen 2019-03-26 12:38:50 +01:00
parent ff6bc2c6f7
commit 147ed34402
3 changed files with 166 additions and 24 deletions

117
Cargo.lock generated
View File

@ -1,3 +1,40 @@
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clap"
version = "2.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.50" version = "0.2.50"
@ -37,13 +74,93 @@ dependencies = [
name = "per" name = "per"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"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)", "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "redox_syscall"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "strsim"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "textwrap"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-width"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1" "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" "checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba"
"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -7,3 +7,5 @@ edition = "2018"
[dependencies] [dependencies]
num-bigint = "0.2" num-bigint = "0.2"
num_cpus = "1.0" num_cpus = "1.0"
clap = "2.32.0"

View File

@ -1,34 +1,39 @@
extern crate num_bigint; extern crate num_bigint;
// extern crate num_traits; // extern crate num_traits;
extern crate num_cpus; extern crate num_cpus;
#[macro_use]
extern crate clap;
use clap::App;
use num_bigint::BigUint; use num_bigint::BigUint;
use std::thread; use std::thread;
// use std::sync::mpsc; // use std::sync::mpsc;
use std::sync::{Arc, Barrier, RwLock}; use std::sync::{Arc, Barrier, RwLock};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
trait Stuff {
fn factorize(&self) -> Self; trait Peristance {
fn test(&self) -> u8; fn per_mul(&self) -> u8;
} }
impl Stuff for BigUint { impl Peristance for BigUint {
fn factorize(&self) -> BigUint { fn per_mul(&self) -> u8 {
let string = self.to_str_radix(10); let mut n = self.to_radix_le(10)
let numbers: Vec<u8> = string.chars().map(|x| x as u8 - 48).collect(); .into_iter()
numbers.into_iter().product::<BigUint>() .product::<BigUint>();
}
fn test(&self) -> u8 {
let mut n = self.factorize();
let mut counter = 1; let mut counter = 1;
while n.to_str_radix(10).chars().count() > 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; counter += 1;
} }
return counter; return counter;
@ -61,23 +66,40 @@ impl Iterator for Counter {
fn main() { fn main() {
let start = Instant::now(); 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)); let barrier = Arc::new(Barrier::new(2));
for thread in 0..(cpus) { for thread in 0..(cpus) {
let c = barrier.clone(); let c = barrier.clone();
thread::spawn(move || { thread::spawn(move || {
println!("Started thread {}!", thread); println!("Started thread {}!", thread);
let counter = Counter::new(cpus-thread, cpus); let counter = Counter::new(cpus-thread, cpus);
let mut max: u8 = 0; let mut top: u8 = 0;
for x in counter { for x in counter {
let n = x.test(); let n = x.per_mul();
if n > max { if n > top {
max = n; top = n;
println!("{} - {}: {}", thread, n, x); println!("{:?} {} - {}: {}", start.elapsed(), thread, n, x);
if n == 10 { if n > max-1 {
c.wait(); c.wait();
return; return;
} }
@ -86,6 +108,7 @@ fn main() {
}); });
} }
barrier.wait(); barrier.wait();
println!("Finding n = 10 took {:#?}", start.elapsed());
println!("Finding took {:#?}", start.elapsed());
return; return;
} }