a-conjecture-of-mine

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

Commit
21557580aaf33a3a654791a4d04887367c2d3ab6
Parent
0e844981908c1d1db20730edbd6c1aa2a33718c1
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Optimized the is_multiple_of_nine(n: i32) function.

Diffstat

1 file changed, 19 insertions, 12 deletions

Status File Name N° Changes Insertions Deletions
Modified src/main.rs 31 19 12
diff --git a/src/main.rs b/src/main.rs
@@ -1,7 +1,7 @@
 // The following program is a simple test for the following conjecture:
 
 // 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 interger.
+// For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.
 
 extern crate crossterm;
 
@@ -12,6 +12,7 @@ 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;
 
@@ -32,40 +33,45 @@ fn main() {
     println!("This program is a simple test for the following conjecture:
     
 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 interger.");
+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());
 
     match user_input.trim().parse::<i32>() {
         Ok(max) => {
-            println!("\nLOADING...");
+            let start_time = Instant::now();
+            println!("\nLOADING. . .");
             let counterexpls = get_all_countrexpls(max, n_threads);
+            let duration = start_time.elapsed();
 
             // Print the results
             prompt.clear(ClearType::All);
-            println!("LOADED... 100%\n");
+            println!("LOADED. . . 100% in {}s\n", duration.as_secs());
             if counterexpls.len() == 0 {
                 println!("The conjecture is proved for all natural numbers smaller or equals to {}!\n", max);
             } else {
                 println!("The conjecture is disproved! Here are the counter examples:");
 
+                let mut counterexpls_str = String::new();
+
                 for pair in counterexpls {
-                    println!("{} and {}", pair[0], pair[1]);
+                    let ordered_pair = format!("({}, {})", pair[0], pair[1]);
+                    counterexpls_str = format!("{}, {}", counterexpls_str, ordered_pair);
                 }
 
-                println!("");
+                println!("{}\n", counterexpls_str);
             }
 
             pause_prompt();
         }, 
-        Err(_) => println!("'{}' is not an interger!", user_input.trim())
+        Err(_) => println!("'{}' is not an integer!", user_input.trim())
     }
 }
 
 fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
 
-    if max > 1000 {
+    if max > n_threads * 100 {
 
         // Thread related variables
         let (coutexpl_sender, coutexpl_reciever): (Sender<Vec<[i32; 2]>>, Receiver<Vec<[i32; 2]>>) = mpsc::channel();
@@ -75,7 +81,7 @@ fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
         // Conjecture related variables
         let mut counterexpls: Vec<[i32; 2]> = Vec::new();
 
-        for i in 0..n_threads {
+        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};
@@ -87,7 +93,7 @@ fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
             child_threads.push(child);
         }
 
-        for _ in 0..n_threads {
+        for _ in 1..n_threads {
             counterexpls.append(&mut coutexpl_reciever.recv().unwrap());
         }
 
@@ -119,9 +125,10 @@ fn get_range_countrexpls(range: Range<i32>, max: i32) -> Vec<[i32; 2]> {
 }
 
 fn is_multiple_of_nine(n: i32) -> bool {
-    let n_float = n as f32;
+    let floor = n / 9;
+    let neerest_mult = floor * 9;
 
-    return (n_float/9f32) % 1f32 == 0f32;
+    return n - neerest_mult == 0;
 }
 
 fn get_digits(n: i32) -> Vec<u32> {