a-conjecture-of-mine

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

Commit
b033617f3443e322d7b21f0944c4183fb5651221
Parent
f4a708aee096ffa86edbcfc1a5d4ce037aeb1d1a
Author
Pablo Escobar Gaviria <gark.garcia@protonmail.com>
Date

Finished writing the Racket implementation.

Diffstat

1 file changed, 21 insertions, 6 deletions

Status File Name N° Changes Insertions Deletions
Modified script.rkt 27 21 6
diff --git a/script.rkt b/script.rkt
@@ -1,16 +1,31 @@
+#lang racket
+
+(provide counterexempl)
+
 (define (counterexempl max)
-    (foldl (lambda (acc a) (and acc (iter a))) (in-range max)))
+    (let ([sums-cache (get-sums max)])
+         (not
+              (foldl (lambda (acc a) (and acc (iter a sums-cache)))
+                     #t
+                     (in-range max)))))
 
-(define (iter a)
-    (foldl (lambda (acc b) (and acc (test a b))) (in-range a)))
+(define (iter a sums-cache)
+    (foldl (lambda (acc b) (and acc (test a b sums-cache))) #t (in-range a)))
 
-(define (test a b)
-    (zero? (mod (- (sum-digits (+ a b)) (sum-digits a) (sum-digits b)) 9)))
+(define (test a b sums-cache)
+    (let ([sum-digits (lambda (n) (vector-ref sums-cache n))])
+         (zero?
+             (remainder 
+                 (- (sum-digits (+ a b)) (sum-digits a) (sum-digits b)) 
+                 9))))
 
 (define (sum-digits n) (sum-digits-tail n 0))
 
 (define (sum-digits-tail n acc)
     (if (zero? n)
         acc
-        (sum-digits-tail (div n 10) (+ acc (mod n 10)))))
+        (sum-digits-tail (floor n 10) (+ acc (remainder n 10)))))
+
+(define (get-sums max)
+    (build-vector (+ (* 2 max) 1) sum-digits))