a-conjecture-of-mine

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

commit 6b73ae0b1eb3a5d3144291d9b4df55912e0ef1ef
parent a9b8ef4734d14a7ba588af776b452d8345ac40df
Author: Pablo Emílio Escobar Gaviria <pablo-escobar@riseup.net>
Date:   Wed,  5 Aug 2020 23:48:56 -0300

Patched the scripts

Diffstat:
M.gitignore | 11++---------
MMakefile | 24++++++++++++------------
MREADME.adoc | 6+++---
RC++/main.cpp -> c++/main.cpp | 0
RC/main.c -> c/main.c | 0
RElixir/main.ex -> elixir/main.ex | 0
RExtra/High Level Assembly/conjecture.hla -> extra/High Level Assembly/conjecture.hla | 0
RExtra/x86/PROGRAM.ASM -> extra/x86/PROGRAM.ASM | 0
RExtra/x86/PROGRAM.S -> extra/x86/PROGRAM.S | 0
RGo/main.go -> go/main.go | 0
RHaskell/Main.hs -> haskell/Main.hs | 0
RIdris/Main.idr -> idris/Main.idr | 0
RJava/Main.java -> java/Main.java | 0
RKotlin/main.kt -> kotlin/main.kt | 0
ROCaml/main.ml -> ocaml/main.ml | 0
RRust/main.rs -> rust/main.rs | 0
Mscript.js | 9++-------
Mscript.pl | 10+++++-----
Mscript.py | 17++++++++---------
Mscript.rb | 25+++++++++----------------
Mscript.rkt | 10+++++-----
RWasm/main.wat -> wasm/main.wat | 0
22 files changed, 46 insertions(+), 66 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,14 +1,7 @@
-.idea
-.vscode
 *.img
 *.IMG
 *.beam
 *.jar
 bin
-build
-*.un~
-*.swp
-*.o
-*.hi
-Latex
-Cuda
+latex
+cuda
diff --git a/Makefile b/Makefile
@@ -1,30 +1,30 @@
 c:
-	gcc -pthread -o bin/c C/main.c
+	gcc -pthread -o bin/c ./c/main.c
 
 elixir:
-	elixirc -o bin/elixir Elixir/main.ex
+	elixirc -o bin/elixir ./elixir/main.ex
 
 go:
-	cd Go/ ; go build ; mv ./Go ../bin/go ; cd ..
+	cd ./go/ ; go build ; mv ./go ../bin/go ; cd ..
 
 haskell:
-	ghc --make -O -o bin/haskell Haskell/Main.hs \
-	    && rm Haskell/*.o Haskell/*.hi
+	ghc --make -O -o ./bin/haskell ./haskell/Main.hs \
+	    && rm ./haskell/*.o ./haskell/*.hi
 
 java:
-	javac Java/Main.java
-	jar cfe bin/java.jar Main Java/Main.class && rm Java/*.class
+	javac ./java/Main.java
+	jar cfe ./bin/java.jar Main ./java/Main.class && rm ./java/*.class
 
 kotlin:
-	kotlinc Kotlin/main.kt -d bin/kotlin.jar
+	kotlinc ./kotlin/main.kt -d ./bin/kotlin.jar
 
 ocaml:
-	ocamlfind ocamlopt -thread -package threads -o bin/ocaml -linkpkg OCaml/main.ml \
-	    && rm OCaml/*.cmi OCaml/*.cmx OCaml/*.o
+	ocamlfind ocamlopt -thread -package threads -o ./bin/ocaml -linkpkg ./ocaml/main.ml \
+	    && rm ./ocaml/*.cmi ./ocaml/*.cmx ./ocaml/*.o
 
 rust:
-	rustc -v -o bin/rust Rust/main.rs
+	rustc -v -o ./bin/rust ./rust/main.rs
 
 wasm:
-	wat2wasm Wasm/main.wat -o bin/wasm.wasm
+	wat2wasm ./wasm/main.wat -o ./bin/wasm.wasm
 
diff --git a/README.adoc b/README.adoc
@@ -71,7 +71,7 @@ out.
 However, to ensure we are _comparing apples to apples_ a similar algorithm 
 should be used on all implementations. 
 
-## The Algorithm
+=== The Algorithm
 
 The program should test the conjecture **for all pairs `(a, b)` where 
 `0 <= a <= MAX` and `0 <= b <= a`**, as this is sufficient to proof the 
@@ -93,7 +93,7 @@ exit the process with exit code 0
 
 Alternativelly, one could iterate `b` between `a` and `MAX`.
 
-### Concurency
+=== Concurency
 
 The use of concurrency is encoraged for implementations on languages that 
 provide good _standard_ support for it. In the case concurrency is used, an 
@@ -133,7 +133,7 @@ This scheme ensures computations are envenlly distributed across processes.
 Note that the routine should only signal wheter or not a counterexample was 
 found.
 
-### Caching
+=== Caching
 
 When checking if the conjecture holds for the pair `(a, b)`, the program will
 need to calculate the sum of the digits of `a`, `b` and `a + b`. To avoid 
diff --git a/C++/main.cpp b/c++/main.cpp
diff --git a/C/main.c b/c/main.c
diff --git a/Elixir/main.ex b/elixir/main.ex
diff --git a/Extra/High Level Assembly/conjecture.hla b/extra/High Level Assembly/conjecture.hla
diff --git a/Extra/x86/PROGRAM.ASM b/extra/x86/PROGRAM.ASM
diff --git a/Extra/x86/PROGRAM.S b/extra/x86/PROGRAM.S
diff --git a/Go/main.go b/go/main.go
diff --git a/Haskell/Main.hs b/haskell/Main.hs
diff --git a/Idris/Main.idr b/idris/Main.idr
diff --git a/Java/Main.java b/java/Main.java
diff --git a/Kotlin/main.kt b/kotlin/main.kt
diff --git a/OCaml/main.ml b/ocaml/main.ml
diff --git a/Rust/main.rs b/rust/main.rs
diff --git a/script.js b/script.js
@@ -15,12 +15,7 @@ function sumDigits(n) {
 }
 
 function getSums(max) {
-    const output = [], maxRange = 2 * max + 1;
-    
-    for (let i = 0; i <= maxRange; i++)
-        output.push(sumDigits(i));
-
-    return output;
+    return Array.from({length: 2 * max + 1}, (_, i) => sumDigits(i));
 }
 
 export function counterexempl(max) {
@@ -28,7 +23,7 @@ export function counterexempl(max) {
 
     for (let a = 1; a <= max; a++)
         for (let b = a; b <= max; b++) {
-            const diff = sums[a + b] - (sums[a] + sums[b]);
+            const diff = sums[a + b] - sums[a] - sums[b];
             if (diff % 9 !== 0) return true;
         }
 
diff --git a/script.pl b/script.pl
@@ -3,14 +3,14 @@
 % 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 integer.
 
-sum_digits(X, Y) :- sum_digits_tail(X, 0, Y).
+sum_digits(X, Y) :- sum_digits_acc(X, 0, Y).
 
-sum_digits_tail(0, Acc, Y) :- Y =:= Acc.
-sum_digits_tail(X, Acc, Y) :-
+sum_digits_acc(0, Acc, Acc) :- !.
+sum_digits_acc(X, Acc, Y) :-
     Q = X div 10,
     R = X mod 10,
-    Acc1 = Acc + R,
-    sum_digits(Q, Acc1, Y).
+    Acc1 = Acc + R,             % Accumulate value
+    sum_digits_acc(Q, Acc1, Y). % Propagate Y (result) from recursion back up
 
 test_pair(A, B) :-
     R  = 0,
diff --git a/script.py b/script.py
@@ -13,17 +13,16 @@ def sum_digits(n: int) -> int:
 
     return sum_d
 
-def get_sums(max: int) -> list:
-    return [sum_digits(i) for i in range(2 * max)]
+def get_sums(m: int) -> list:
+    return [sum_digits(i) for i in range(2 * m)]
 
-def counterexempl(max: int) -> bool:
-    sums = get_sums(max)
+def counterexempl(m: int) -> bool:
+    sums = get_sums(m)
 
-    for a in range(max + 1):
-        for b in range(a, max + 1):
+    for a in range(m + 1):
+        for b in range(a, m + 1):
             diff = sums[a + b] - sums[a] - sums[b]
+            if diff % 9 != 0: return True
 
-            if not diff % 9 == 0:
-                return True
-    
     return False
+
diff --git a/script.rb b/script.rb
@@ -3,12 +3,12 @@
 # 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.
 
-def sum_digits(i)
+def sum_digits i
     part = i.abs()
     sum = 0
 
     while part > 0
-        d, r = part.divmod(10)
+        d, r = part.divmod 10
         sum += r
         part = d
     end
@@ -16,24 +16,17 @@ def sum_digits(i)
     sum
 end
 
-def get_sums(max)
-    output = []
-    
-    for i in 0..(2 * max + 1)
-        output << sum_digits(i)
-    end
+def get_sums max
+    (0 .. 2 * max).map {|i| sum_digits i}
 end
 
-def counterexempl(max)
-    sums = get_sums(max)
+def counterexempl max
+    sums = get_sums max
 
-    for a in (0..max)
-        for b in (a..max)
+    for a in 0..max
+        for b in a..max
             diff = sums[a + b] - sums[a] - sums[b]
-
-            if diff % 9 != 0
-                return true
-            end
+            return true if diff % 9 != 0
         end
     end
 
diff --git a/script.rkt b/script.rkt
@@ -7,13 +7,13 @@
 
 (provide counterexempl?)
 
-;; Returns `#t` if a counterexemple was found between `0` and `max`.
+;; Returns `#t` if a counterexemple was found between `0` and `m`.
 ;; Otherwise returns `#f`.
-(define (counterexempl? max)
-    (let ([sums-cache (get-sums max)])
+(define (counterexempl? m)
+    (let ([sums-cache (get-sums m)])
          (not (stream-fold (lambda (acc a) (and acc (iter a sums-cache)))
                            #t
-                           (in-range max)))))
+                           (in-range m)))))
 
 (define (iter a sums-cache)
     (stream-fold (lambda (acc b) (and acc (test a b sums-cache))) 
@@ -31,5 +31,5 @@
             (sum-digits-tail (quotient n 10) (+ acc (remainder n 10)))))
     (sum-digits-tail n 0))
 
-(define (get-sums max) (build-vector (+ (* 2 max) 1) sum-digits))
+(define (get-sums m) (build-vector (+ (* 2 m) 1) sum-digits))
 
diff --git a/Wasm/main.wat b/wasm/main.wat