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