a-conjecture-of-mine

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

main.cpp (954B)

 1 #include <iostream>
 2 #include <string>
 3 #include <thread>
 4 #include <map>
 5 #include <vector>
 6 
 7 #define SUCCESS 0
 8 #define FAIL 1
 9 #define INVALID_INPUT 2
10 
11 inline int sum_digits(unsigned int n)
12 {
13     unsigned int sum = 0;
14 
15     while (n != 0)
16     {
17         sum += (n % 10);
18         n /= 10;
19     }
20 
21     return sum;
22 }
23 
24 bool counterexpl(unsigned int max, std::vector<int> sums_cache)
25 {
26     for (auto a = 0; a <= max; a ++)
27         for (auto b = a; b <= max; b++)
28             if ((sums_cache[a + b] - sums_cache[a] - sums_cache[b]) % 9 != 0)
29                 return true;
30 
31     return false;
32 }
33 
34 int main(int argc, char *argv[])
35 {
36     if (argc < 2) return INVALID_INPUT;
37     unsigned int max = std::stoul(argv[1]);
38 
39     std::vector<int> sums_cache;
40     
41     // Builds the sums cache
42     for (int i = 0; i <= 2 * max; i++)
43         sums_cache[i] = sum_digits(i);
44 
45     return counterexpl(max, sums_cache) ? FAIL : SUCCESS;
46 }
47