a-conjecture-of-mine

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

main.wat (3580B)

  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 (module
  7     ;; Check if there is any counterexample 
  8     ;; in {(x, y) | 0 <= x <= a, 0 <= y <= x}
  9     (func (export "counterexempl") (param $a i32) (result i32)
 10         (local $b i32)
 11 
 12         ;; while a != 0
 13         (block $break_0             
 14                (loop $while_0
 15                      (br_if $break_0 (i32.eqz (local.get $a)))
 16 
 17                      ;; b = a
 18                      (local.set $b (local.get $a))
 19 
 20                      ;; while b != 0 
 21                      (block $break_1                                         
 22                             (loop $while_1
 23                                   (br_if $break_1 (i32.eqz (local.get $b)))
 24                                   (if ;; if test(a, b)
 25                                     (call $test (local.get $a) (local.get $b))
 26                                     
 27                                     ;; return 1                  
 28                                     (block (i32.const 1) (return))                              
 29                                     
 30                                     (block ;; else
 31 
 32                                       ;; b -= 1
 33                                       (local.set $b 
 34                                                  (i32.sub 
 35                                                    (local.get $b) 
 36                                                    (i32.const 1)
 37                                                  )
 38                                       ) 
 39                                       (br $while_1) ;; continue
 40                                     )
 41                                   )
 42                             )
 43                      )
 44 
 45                      ;; a -= 1
 46                      (local.set $a (i32.sub (local.get $a) (i32.const 1))) 
 47                      (br $while_0) ;; continue
 48                )
 49         )
 50 
 51         (i32.const 0) ;; return 0
 52     )
 53 
 54     ;; Calculates the sums of the digits of a non-negative integer.
 55     (func $sum_digits (param $n i32) (result i32)
 56         (local $sum i32)
 57         (local.set $sum (i32.const 0))
 58 
 59         (block $break
 60                (loop $while    
 61                      ;; Break if n == 0i32
 62                      (br_if $break (i32.eqz (local.get $n)))
 63                
 64                      ;; sum += n % 10
 65                      (local.set $sum 
 66                                 (i32.add
 67                                   (local.get $sum)
 68                                   (i32.rem_u (local.get $n) (i32.const 10))
 69                                 )
 70                      )
 71                      
 72                      ;; n /= 10
 73                      (local.set $n (i32.div_u (local.get $n) (i32.const 10)))
 74 
 75                      ;; Go to `while`
 76                      (br $while)
 77                )
 78         )
 79 
 80         (local.get $sum) ;; return sum
 81     )
 82 
 83     ;; Checks if (sum_digits(a + b) - sum_digits(a) - sum_digits(b)) % 9 != 0
 84     (func $test (export "test") (param $a i32) (param $b i32) (result i32) 
 85           (i32.ne
 86             (i32.const 0) 
 87             (i32.rem_s
 88               (i32.sub 
 89                 (call $sum_digits (i32.add (local.get $a) (local.get $b)))
 90                 (i32.add
 91                   (call $sum_digits (local.get $a))
 92                   (call $sum_digits (local.get $b))
 93                 )
 94               )
 95               (i32.const 9)
 96             )
 97           )
 98     )
 99 )
100 
101