a-conjecture-of-mine

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

script.rkt (1142B)

 1 #lang racket
 2 
 3 ; This script is a simple test for the following conjecture:
 4 
 5 ; Let S: N -> N be the sum of the digits of a positive integer.
 6 ; For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.
 7 
 8 (provide counterexempl?)
 9 
10 ;; Returns `#t` if a counterexemple was found between `0` and `m`.
11 ;; Otherwise returns `#f`.
12 (define (counterexempl? m)
13     (define (test a b sums-cache)
14         (let ([sum (lambda (n) (vector-ref sums-cache n))])
15              (zero? (remainder (- (sum (+ a b)) (sum a) (sum b)) 9))))
16     (define (iter a sums-cache)
17         (stream-fold (lambda (acc b) (and acc (test a b sums-cache))) 
18                      #t 
19                      (in-range a)))
20     (let ([sums-cache (get-sums m)])
21          (not (stream-fold (lambda (acc a) (and acc (iter a sums-cache)))
22                            #t
23                            (in-range m)))))
24 
25 (define (sum-digits n) 
26     (define (sum-digits-tail n acc)
27         (if (zero? n)
28             acc
29             (sum-digits-tail (quotient n 10) (+ acc (remainder n 10)))))
30     (sum-digits-tail n 0))
31 
32 (define (get-sums m) (build-vector (+ (* 2 m) 1) sum-digits))
33