a-conjecture-of-mine

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

Commit
2b5b6fe1ae108a9c867ae307fe66d61c6fcf8c0a
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Initial commit.

Diffstat

4 files changed, 80 insertions, 0 deletions

Status File Name N° Changes Insertions Deletions
Added .gitignore 2 2 0
Added Cargo.lock 4 4 0
Added Cargo.toml 6 6 0
Added src/main.rs 68 68 0
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,2 @@
+/target
+**/*.rs.bk
diff --git a/Cargo.lock b/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "conjecture_tester"
+version = "0.1.0"
+
diff --git a/Cargo.toml b/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "conjecture_tester"
+version = "0.1.0"
+authors = ["Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>"]
+
+[dependencies]
diff --git a/src/main.rs b/src/main.rs
@@ -0,0 +1,68 @@
+// The following script 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.
+
+fn main() {
+    let max : i32 = 1000;
+    let counter_examples : Vec<[i32; 2]> = test_conjecture(max);
+
+    // Print the results
+    if counter_examples.len() == 0 {
+        println!("The conjecture holds up to {}!", max);
+    } else {
+        println!("The conjecture doesn't hold! Here are the counter examples:");
+
+        for pair in counter_examples {
+            println!("{} and {};", pair[0], pair[1]);
+        }
+    }
+}
+
+// Test the conjecture for all values up to max and return the counterexamples
+fn test_conjecture(max : i32) -> Vec<[i32; 2]> {
+    let mut counter_examples : Vec<[i32; 2]> = Vec::new();
+    let mut load_bar = 0f32;
+
+    for a in 0..max {
+        for b in a..max {
+            let difference : i32 = sum_digits(a + b) - sum_digits(a) - sum_digits(b);
+
+            if !is_multiple_of_nine(difference) {
+                counter_examples.push([a, b]);
+            }
+
+            // Print the progress on the screen
+            let new_load_bar = ((a as f32) * 100f32 / (max as f32)).ceil();
+
+            if new_load_bar != load_bar {
+                load_bar = new_load_bar;
+                print!("{}[2J", 27 as char);
+                println!("LOADING: {}%", new_load_bar);
+            }
+        }
+    }
+
+    return counter_examples;
+}
+
+fn is_multiple_of_nine(n : i32) -> bool {
+    let n_float = n as f32;
+
+    return (n_float/9f32) % 1f32 == 0f32;
+}
+
+fn get_digits(n : i32) -> Vec<u32> {
+    return n.to_string().chars().map(|d| d.to_digit(10).unwrap()).collect();
+}
+
+fn sum_digits(n : i32) -> i32 {
+    let mut sum : i32 = 0i32;
+
+    for d in get_digits(n) {
+        let d_ = d as i32;
+        sum += d_;
+    }
+
+    return sum;
+}