a-conjecture-of-mine

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

commit cdfc38f2a95be7bc519ca2506102e77990ea3f77
parent f37336eec8f051a71cc76a10a543bd69c1c0c3e7
Author: Pablo Escobar Gaviria <gark.garcia@protonmail.com>
Date:   Sat, 11 Jan 2020 18:07:02 -0200

Pulled `Wasm/main.wat` from `github`.

Diffstat:
AWasm/.gitignore | 2++
AWasm/main.wat | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DWasm/test | 13-------------
3 files changed, 78 insertions(+), 13 deletions(-)
diff --git a/Wasm/.gitignore b/Wasm/.gitignore
@@ -0,0 +1,2 @@
+test
+*.wasm
diff --git a/Wasm/main.wat b/Wasm/main.wat
@@ -0,0 +1,76 @@
+(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)
+        )
+    )
+    )
+)
+
+
diff --git a/Wasm/test b/Wasm/test
@@ -1,13 +0,0 @@
-#!/usr/bin/node
-
-const fs = require("fs");
-
-const src = new Uint8Array(fs.readFileSync("./main.wasm"));
-WebAssembly.instantiate(src).then(test).catch(console.error);
-
-function test(mod) {
-    const result =  mod.instance.exports.counterexempl(100);
-
-    console.log(`RESULT: ${result}`);
-}
-