a-conjecture-of-mine

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

commit 4816721253ecc263363101e7075f794e52044c9e
parent 994bfc08df44c1371d43323b5837199354295807
Author: Pablo Emilio Escobar Gavíria <pablo-escobar@riseup.net>
Date:   Sat, 15 Aug 2020 08:06:57 -0300

Fixed stuff

Diffstat:
Mhaskell/Main.hs | 4++--
Mjava/Main.java | 3++-
Mscript.rb | 6+++---
3 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/haskell/Main.hs b/haskell/Main.hs
@@ -36,8 +36,8 @@ sumDigits = sumDigitsTail 0
 -- Otherwise returns `False`.
 test' :: Vector Int -> (Int, Int) -> Bool
 test' sums (a, b) = diff `mod` 9 == 0
-    where diff = sums ! (a + b) - sums ! a - sums ! b
-          (!) = unsafeIndex
+    where diff = sums !! (a + b) - sums !! a - sums !! b
+          (!!) = unsafeIndex
 
 -- Checks if there is any counterexample in
 -- [(a, b) | a <- [0..max], b <- [a..max]].
diff --git a/java/Main.java b/java/Main.java
@@ -13,6 +13,7 @@ public class Main {
     public static void main(String[] args) {
         try {
             int max = Integer.parseInt(args[0]);
+            if (max <= 0) throw IllegalArgumentException();
 
             if (counterexample(max)) {
                 System.exit(FAIL);
@@ -30,7 +31,7 @@ public class Main {
 
         for (int a = 0; a <= max; a++)
             for (int b = a; b <= max; b++) {
-                int diff = sum[a + b] - (sum[a] + sum[b]);
+                int diff = sum[a + b] - sum[a] - sum[b];
 
                 if (diff % 9 != 0)
                     return true;
diff --git a/script.rb b/script.rb
@@ -20,12 +20,12 @@ def get_sums max
     (0 .. 2 * max).map {|i| sum_digits i}
 end
 
-def counterexempl max
-    sums = get_sums max
+def counterexempl? max
+    sums_cache = get_sums max
 
     for a in 0..max
         for b in a..max
-            diff = sums[a + b] - sums[a] - sums[b]
+            diff = sums_cache[a + b] - sums_cache[a] - sums_cache[b]
             return true if diff % 9 != 0
         end
     end