a-conjecture-of-mine

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

script.rb (701B)

 1 # This script is a simple test for the following conjecture:
 2 
 3 # Let S: N -> N be the sum of the digits of a positive integer.
 4 # For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.
 5 
 6 def sum_digits i
 7     part = i.abs()
 8     sum = 0
 9 
10     while part > 0
11         d, r = part.divmod 10
12         sum += r
13         part = d
14     end
15 
16     sum
17 end
18 
19 def get_sums max
20     (0 .. 2 * max).map {|i| sum_digits i}
21 end
22 
23 def counterexempl? max
24     sums_cache = get_sums max
25 
26     for a in 0..max
27         for b in a..max
28             diff = sums_cache[a + b] - sums_cache[a] - sums_cache[b]
29             return true if diff % 9 != 0
30         end
31     end
32 
33     false
34 end
35