diff --git a/src/main.rs b/src/main.rs
@@ -5,17 +5,18 @@
extern crate crossterm;
extern crate num_cpus;
+extern crate rand;
use std::io::{stdin, stdout, prelude::*};
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc;
use std::thread;
-use std::ops::Range;
use std::env;
use std::process::Command;
use std::time::Instant;
use crossterm::terminal::{terminal,ClearType};
use crossterm::Screen;
+use rand::prelude::*;
fn main() {
let args: Vec<String> = env::args().collect();
@@ -24,9 +25,9 @@ fn main() {
// Assign the correct number of threads to run the application with
// The default is the number of cores in the machine
- let n_cores = num_cpus::get_physical() as i32;
+ let n_cores = num_cpus::get_physical() as i64;
let n_threads = if args.len() == 0 { n_cores } else {
- match args[0].trim().parse::<i32>() {
+ match args[0].trim().parse::<i64>() {
Ok(n) => std::cmp::min(n.abs(), n_cores),
Err(_) => n_cores
}
@@ -40,7 +41,7 @@ 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?");
- match user_input.trim().parse::<i32>() {
+ match user_input.trim().parse::<i64>() {
Ok(max) => {
let start_time = Instant::now();
println!("\nLOADING. . .");
@@ -71,26 +72,32 @@ 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]> {
+fn get_all_countrexpls(max: i64, n_threads: i64) -> Vec<[i64; 2]> {
if n_threads < 1 { panic!("The number {} is not a valid number of threads.", n_threads) }
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();
+ let (coutexpl_sender, coutexpl_reciever): (Sender<Vec<[i64; 2]>>, Receiver<Vec<[i64; 2]>>) = mpsc::channel();
let mut child_threads = Vec::new();
- let range_lenght = ((max as f32) / n_threads as f32).ceil() as i32;
+ let range_lenght = ((max as f64) / n_threads as f64).ceil() as i64;
+ let mut range: Vec<i64> = (0..max).collect();
// Conjecture related variables
- let mut counterexpls: Vec<[i32; 2]> = Vec::new();
+ let mut counterexpls: Vec<[i64; 2]> = Vec::new();
+
+ // Shuffle the values in the range to get an even distribution of calculations across all threads
+ range.shuffle(&mut thread_rng());
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 start = (i - 1) * range_lenght;
+ let end = start + range_lenght - 1;
+ let thread_range = range[(start as usize)..(end as usize)].to_vec();
let child = thread::spawn(move || {
- thread_countr_sd.send(get_range_countrexpls(range, max))
+ thread_countr_sd.send(get_range_countrexpls(thread_range, max))
.expect(&*format!("Thread n°{} was unable to sent a message trought the channel", i));
});
child_threads.push(child);
@@ -107,16 +114,16 @@ fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
return counterexpls;
} else {
- return get_range_countrexpls(0..max, max);
+ return get_range_countrexpls((0..max).collect(), max);
}
}
-fn get_range_countrexpls(range: Range<i32>, max: i32) -> Vec<[i32; 2]> {
+fn get_range_countrexpls(range: Vec<i64>, max: i64) -> Vec<[i64; 2]> {
let mut counterexpls = Vec::new();
for a in range {
for b in a..max {
- let difference: i32 = sum_digits(a + b) - sum_digits(a) - sum_digits(b);
+ let difference: i64 = sum_digits(a + b) - sum_digits(a) - sum_digits(b);
if !is_multiple_of_nine(difference) {
counterexpls.push([a, b]);
@@ -127,23 +134,22 @@ fn get_range_countrexpls(range: Range<i32>, max: i32) -> Vec<[i32; 2]> {
return counterexpls;
}
-fn is_multiple_of_nine(n: i32) -> bool {
+fn is_multiple_of_nine(n: i64) -> bool {
let floor = n / 9;
let neerest_mult = floor * 9;
return n == neerest_mult;
}
-fn get_digits(n: i32) -> Vec<u32> {
- return n.to_string().chars().map(|d| d.to_digit(10).unwrap()).collect();
+fn get_digits(n: i64) -> Vec<i64> {
+ return n.to_string().chars().map(|d| d.to_digit(10).unwrap() as i64).collect();
}
-fn sum_digits(n: i32) -> i32 {
+fn sum_digits(n: i64) -> i64 {
let mut sum = 0;
for d in get_digits(n) {
- let _d = d as i32;
- sum += _d;
+ sum += d;
}
return sum;