a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

commit b35f3b6b80ecaab097c95dba99993bcd877ed261
parent 21557580aaf33a3a654791a4d04887367c2d3ab6
Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date:   Sun, 11 Nov 2018 19:37:48 -0200

Optimized threading functionalities.

Constrained the n_threads variable to the number of cores in the machine and made it the default value of n_threads.

Diffstat:
MCargo.lock | 10++++++++++
MCargo.toml | 5+++--
Dfdsfs | 29-----------------------------
Msrc/main.rs | 35+++++++++++++++++++----------------
4 files changed, 32 insertions(+), 47 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -3,6 +3,7 @@ name = "conjecture_tester"
 version = "0.1.0"
 dependencies = [
  "crossterm 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -21,6 +22,14 @@ version = "0.2.43"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "num_cpus"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "termios"
 version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -50,6 +59,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 [metadata]
 "checksum crossterm 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3069b1b614f818b56d12eabb91d3fe39bfedb40d99f111b07969ac86039ce60e"
 "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
+"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
 "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625"
 "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"
diff --git a/Cargo.toml b/Cargo.toml
@@ -4,4 +4,5 @@ version = "0.1.0"
 authors = ["Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>"]
 
 [dependencies]
-crossterm = "0.4.0"-
\ No newline at end of file
+crossterm = "0.4.0"
+num_cpus = "1.0"+
\ No newline at end of file
diff --git a/fdsfs b/fdsfs
@@ -1,29 +0,0 @@
-commit fbd0555b91f701ea72f6027d4d7d7a1c36540ffd (HEAD -> master)
-Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
-Date:   Sat Nov 3 16:29:00 2018 -0300
-
-    Implemented multi-threading functionality.
-
-commit adfd52d53c6f4a6b1e0a0dd1cbf5b06f0afdf470
-Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
-Date:   Fri Nov 2 19:19:53 2018 -0300
-
-    Cleaned unecessary type conversions.
-
-commit 40b7cae777a49cbc6d369b6a003c4324a0b36cd0
-Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
-Date:   Fri Nov 2 18:42:43 2018 -0300
-
-    Optimized the progress annotation functionality.
-
-commit 836609a62dd778474a80512994c36840b5b4ac23
-Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
-Date:   Fri Nov 2 17:49:25 2018 -0300
-
-    Implemented console functionalities.
-
-commit 2b5b6fe1ae108a9c867ae307fe66d61c6fcf8c0a
-Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
-Date:   Wed Oct 31 15:09:47 2018 -0300
-
-    Initial commit.
diff --git a/src/main.rs b/src/main.rs
@@ -4,6 +4,7 @@
 // For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.
 
 extern crate crossterm;
+extern crate num_cpus;
 
 use std::io::{stdin, stdout, prelude::*};
 use std::sync::mpsc::{Sender, Receiver};
@@ -22,12 +23,13 @@ fn main() {
     let prompt = terminal(&screen);
 
     // Assign the correct number of threads to run the application with
-    // The default is 10
-    let n_threads = if args.len() == 0 { 10 } else {
-         match args[0].trim().parse::<i32>() {
-             Ok(n) => n,
-             Err(_) => 10
-         }
+    // The default is the number of cores in the machine
+    let n_cores = num_cpus::get_physical() as i32;
+    let n_threads = if args.len() == 0 { n_cores } else {
+        match args[0].trim().parse::<i32>() {
+            Ok(n) => std::cmp::min(n.abs(), n_cores),
+            Err(_) => n_cores
+        }
     };
 
     println!("This program is a simple test for the following conjecture:
@@ -36,20 +38,20 @@ Let S: N -> N be the sum of the digits of a positive integer.
 For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.");
 
     // Listen for user input
-    let user_input = ask("\nWhat value would you like to test the conjecture for? ".to_owned());
+    let user_input = ask("\nWhat value would you like to test the conjecture for?");
 
     match user_input.trim().parse::<i32>() {
         Ok(max) => {
             let start_time = Instant::now();
             println!("\nLOADING. . .");
-            let counterexpls = get_all_countrexpls(max, n_threads);
+            let counterexpls = get_all_countrexpls(max.abs(), n_threads);
             let duration = start_time.elapsed();
 
             // Print the results
             prompt.clear(ClearType::All);
-            println!("LOADED. . . 100% in {}s\n", duration.as_secs());
+            println!("LOADED. . . 100% in {}s [{} threads]\n", duration.as_secs(), n_threads);
             if counterexpls.len() == 0 {
-                println!("The conjecture is proved for all natural numbers smaller or equals to {}!\n", max);
+                println!("The conjecture is proved for all natural numbers smaller or equals to {}!", max);
             } else {
                 println!("The conjecture is disproved! Here are the counter examples:");
 
@@ -70,8 +72,9 @@ For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.");
 }
 
 fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
+    if n_threads < 1 { panic!("The number {} is not a valid number of threads.", n_threads) }
 
-    if max > n_threads * 100 {
+    if max / n_threads > 0 && n_threads > 1 {
 
         // Thread related variables
         let (coutexpl_sender, coutexpl_reciever): (Sender<Vec<[i32; 2]>>, Receiver<Vec<[i32; 2]>>) = mpsc::channel();
@@ -84,11 +87,11 @@ fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
         for i in 1..n_threads {
             let thread_countr_sd = coutexpl_sender.clone();
             let end = std::cmp::min(i * (range_lenght + 1) + range_lenght, max);
-            let range = Range {start: i * (range_lenght + 1), end: end};
+            let range = Range { start: i * (range_lenght + 1), end: end };
 
             let child = thread::spawn(move || {
                 thread_countr_sd.send(get_range_countrexpls(range, max))
-                    .expect("The thread was unable to sent a message trought the channel");
+                    .expect(&*format!("Thread n°{} was unable to sent a message trought the channel", i));
             });
             child_threads.push(child);
         }
@@ -128,7 +131,7 @@ fn is_multiple_of_nine(n: i32) -> bool {
     let floor = n / 9;
     let neerest_mult = floor * 9;
 
-    return n - neerest_mult == 0;
+    return n == neerest_mult;
 }
 
 fn get_digits(n: i32) -> Vec<u32> {
@@ -146,9 +149,9 @@ fn sum_digits(n: i32) -> i32 {
     return sum;
 }
 
-fn ask(message: String) -> String {
+fn ask(message: &str) -> String {
     // Print the question
-    write!(stdout(), "{}", message).unwrap();
+    write!(stdout(), "{} ", message).unwrap();
     stdout().flush().unwrap();
 
     // Return the responce