a-conjecture-of-mine

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

Commit
30e02981ed1132422f015e690ff4d18dae305b8e
Parent
3fd6bd1adb62ee1365a27e075a514bb28f3f2ed3
Author
Pablo Escobar Gaviria <gark.garcia@protonmail.com>
Date

Revert "Create a pure Wasm implementation."

This reverts commit 64c1ff4ad588df2ce4d12fd7d5a770a7e07f7527.

Diffstat

5 files changed, 23 insertions, 89 deletions

Status File Name N° Changes Insertions Deletions
Modified C++/main.cpp 3 2 1
Modified C/main.c 33 21 12
Deleted Wasm/.gitignore 1 0 1
Deleted Wasm/main.wasm 0 0 0
Deleted Wasm/main.wat 75 0 75
diff --git a/C++/main.cpp b/C++/main.cpp
@@ -72,4 +72,4 @@ int main(int argc, char *argv[])
         return SUCCESS;
     }
     else return INVALID_INPUT;
-}
+}+
\ No newline at end of file
diff --git a/C/main.c b/C/main.c
@@ -23,14 +23,14 @@
 
 int err = SUCCESS; // Global error condition
 int *sums_cache;   // Shared memory between threads
-unsigned int max = 0;
 
 // Memory structure to pass arguments to threads using the pthreads library
-typedef struct
+struct iter_info
 {
     int start;
-    int step;
-} iter_info;
+    int interval;
+    int max;
+};
 
 // Find the number of processors on host machine
 int get_num_cores()
@@ -76,10 +76,10 @@ int sum_digits(unsigned n)
     return sum;
 }
 
-int get_counterexpl_iter(iter_info *iter)
+int get_counterexpl_iter(struct iter_info *iter)
 {
-    for (int a = iter->start; a <= max; a += iter->step)
-        for (int b = a; b <= max; b++)
+    for (int a = iter->start; a <= iter->max; a += iter->interval)
+        for (int b = a; b <= iter->max; b++)
         {
             if ((sums_cache[a + b] - (sums_cache[a] + sums_cache[b])) % 9 != 0)
             {
@@ -95,12 +95,20 @@ int main(int argc, char *argv[])
 {
     if (argc > 1)
     {
-        max = strtoul(argv[1], NULL, 10);
-	if (max <= 0) return INVALID_INPUT;
+        // Check if argv[1] is numeric
+        int i = 0;
+        while (argv[1][i] != '\0')
+        {
+            if (argv[1][i] < '0' || argv[1][i] > '9')
+                return INVALID_INPUT;
+
+            i++;
+        }
 
+        unsigned int max = strtoul(argv[1], NULL, 10);
         int n_threads = get_num_cores();
         pthread_t thread_ids[n_threads];
-        iter_info thread_iters[n_threads];
+        struct iter_info thread_iters[n_threads];
 
         // Create the sums cache
         int sums_c = 2 * max;
@@ -113,7 +121,9 @@ int main(int argc, char *argv[])
         // of cores on the host machine
         for (int i = 0; i < n_threads; i++)
         {
-	    thread_iters[i] = (iter_info){i, n_threads};
+            thread_iters[i].start = i;
+            thread_iters[i].max = max;
+            thread_iters[i].interval = n_threads;
             err = pthread_create(&thread_ids[i], NULL, get_counterexpl_iter, &thread_iters[i]);
 
             if (err) fprintf(stderr, "Unable to create thread : %d\n", err);
@@ -129,4 +139,3 @@ int main(int argc, char *argv[])
     }
     else return INVALID_INPUT;
 }
-
diff --git a/Wasm/.gitignore b/Wasm/.gitignore
@@ -1 +0,0 @@
-test
diff --git a/Wasm/main.wasm b/Wasm/main.wasm
Binary files differ.
diff --git a/Wasm/main.wat b/Wasm/main.wat
@@ -1,75 +0,0 @@
-(module
-    ;; Check if the is any counterexample in {(x, y) | 0 <= x <= a, 0 <= y <= x}
-    (func (export "counterexempl") (param $a i32) (result i32)
-        (local $b i32)
-
-        (block $break_0     ;; while a != 0
-	    (loop $while_0
-                (br_if $break_0 (i32.eqz (local.get $a)))				
-	        (local.set $b (local.get $a))                           ;; b = a
-
-    	        (block $break_1                                         ;; while b != 0
-	            (loop $while_1
-		        (br_if $break_1 (i32.eqz (local.get $b)))
-			(if (call $test (local.get $a) (local.get $b))                  ;; if test(a, b)
-                            (block (i32.const 1) (return))                              ;; return 1
-                            (block                                                      ;; else
-			        (local.set $b (i32.sub (local.get $b) (i32.const 1)))   ;; b -= 1
-			        (br $while_1)                                           ;; continue
-                            )
-			)
-     		    )
-                )
-    
-	        (local.set $a (i32.sub (local.get $a) (i32.const 1)))   ;; a -= 1
-	        (br $while_0)                                           ;; continue
-	    )
-        )
-
-        (i32.const 0)   ;; return 0
-    )
-
-    ;; Calculates the sums of the digits of a non-negative integer.
-    (func $sum_digits (param $n i32) (result i32)
-        (local $sum i32)
-        (local.set $sum (i32.const 0))
-
-        (block $break
-	    (loop $while	
-	        ;; Break if n == 0i32
-	        (br_if $break (i32.eqz (local.get $n)))
-		
-	        ;; sum += n % 10
-	        (local.set $sum 
-		    (i32.add
-                        (local.get $sum) 
-		        (i32.rem_u (local.get $n) (i32.const 10))
-		    )
-	        )
-
-	        ;; n /= 10
-	        (local.set $n (i32.div_u (local.get $n) (i32.const 10)))
-
-	        ;; Go to `while`
-	        (br $while)
-	    )
-        )
-
-        (local.get $sum)    ;; return sum
-    )
-
-    ;; Checks if (sum_digits(a + b) - (sum_digits(a) + sum_digits(b))) % 9 != 0
-    (func $test (export "test") (param $a i32) (param $b i32) (result i32) 
-        (i32.ne
-            (i32.const 0) 
-	    (i32.rem_s
-		(i32.sub 
-		    (call $sum_digits (i32.add (local.get $a) (local.get $b)))
-		    (i32.add (call $sum_digits (local.get $a)) (call $sum_digits (local.get $b)))
-		) 
-	        (i32.const 9)
-	    )
-	)
-    )
-)
-