a-conjecture-of-mine

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

main.c (1287B)

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 #define SUCCESS 0
 5 #define FAIL 1
 6 #define INVALID_INPUT 2
 7 
 8 // This program is a simple test for the following conjecture:
 9 
10 // Let S: N -> N be the sum of the digits of a positive integer.
11 // For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.
12 
13 int sum_digits(unsigned int n)
14 {
15     int parc = n;
16     int sum = 0;
17 
18     while (parc > 0)
19     {
20         sum += parc % 10;
21         parc /= 10;
22     }
23 
24     return sum;
25 }
26 
27 // Searches for a counterexample in an specific range.
28 int counterexempl(int max, int *sums_cache)
29 {
30     for (int a = 0; a <= max; a++)
31         for (int b = a; b <= max; b++)
32             if ((sums_cache[a + b] - (sums_cache[a] + sums_cache[b])) % 9 != 0)
33                 return FAIL;
34 
35     return SUCCESS;
36 }
37 
38 int main(int argc, char *argv[])
39 {
40     if (argc > 1)
41     {
42         unsigned int max = strtoul(argv[1], NULL, 10);
43 	      if (!max) return INVALID_INPUT;
44 
45         // Create the sums cache
46         int *sums_cache = malloc(sizeof(int) * (2 * max + 1));
47         for (int i = 0; i <= 2 * max + 1; i++)
48             sums_cache[i] = sum_digits(i);
49 
50         return counterexempl(max, sums_cache);
51     }
52     else 
53     {
54         return INVALID_INPUT;
55     }
56 }
57