a-conjecture-of-mine

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

main.rs (1428B)

 1 // The following program 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 integer.
 5 
 6 use std::{env, process};
 7 
 8 const SUCCESS: i32 = 0;
 9 const FAIL: i32 = 1;
10 const INVALID_INPUT: i32 = 2;
11 
12 macro_rules! parse_arg {
13     ($argv: expr) => {{
14         match $argv.next().and_then(|s| s.trim().parse().ok()) {
15             Some(n) => n,
16             None    => process::exit(INVALID_INPUT)
17         }
18     }}
19 }
20 
21 fn main() {
22     let mut args = env::args();
23     args.next();
24 
25     let max = parse_arg!(args);
26 
27     if counterexempl(max) {
28         process::exit(FAIL);
29     } else {
30         process::exit(SUCCESS);
31     }
32 }
33 
34 fn counterexempl(max: usize) -> bool {
35     let sums_cache = get_sums(max);
36 
37     for a in 0..max {
38         for b in a..max {
39             if (sums_cache[a + b] - sums_cache[a] - sums_cache[b]) % 9 != 0 {
40                 return true;
41             }
42         }
43     }
44 
45     false
46 }
47 
48 fn sum_digits(n: usize) -> isize {
49     let mut sum = 0;
50     let mut part = n;
51 
52     while part != 0 {
53         sum += part % 10;
54         part /= 10;
55     }
56 
57     sum as isize
58 }
59 
60 fn get_sums(max: usize) -> Vec<isize> {
61     let mut output = Vec::with_capacity(2 * max);
62 
63     for n in 0..max * 2 {
64         output.push(sum_digits(n));
65     }
66 
67     output
68 }
69