a-conjecture-of-mine

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

script.js (787B)

 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 function sumDigits(n) {
 7     let parc = Math.abs(n), sum = 0;
 8 
 9     while (parc > 0) {
10         sum += parc % 10;
11         parc = Math.floor(parc / 10);
12     }
13 
14     return sum;
15 }
16 
17 function getSums(max) {
18     return Array.from({length: 2 * max}, (_, i) => sumDigits(i));
19 }
20 
21 export function counterexempl(max) {
22     const sums = getSums(max);
23 
24     for (let a = 1; a <= max; a++)
25         for (let b = a; b <= max; b++) {
26             const diff = sums[a + b] - sums[a] - sums[b];
27             if (diff % 9 !== 0) return true;
28         }
29 
30     return false;
31 }
32