ADD = 0
SUB = 1
nexpr = 13122 - 1
len f[] nexpr + 1
arrbase f[] 0
# 
func evaluate code .
   power = 1
   for k = 9 downto 1
      numb += power * k
      m = code mod 3
      if m = ADD
         value += numb
         numb = 0
         power = 1
      elif m = SUB
         value -= numb
         numb = 0
         power = 1
      else
         power *= 10
      .
      code = code div 3
   .
   return value
.
proc init . .
   for i = 0 to nexpr
      f[i] = evaluate i
   .
.
call init
proc out code . .
   a = 19683
   b = 6561
   for k = 1 to 9
      h = code mod a div b
      if h = ADD
         if k > 1
            s$ &= "+"
         .
      elif h = SUB
         s$ &= "-"
      .
      a = b
      b = b div 3
      s$ &= strchar (48 + k)
   .
   print evaluate code & " = " & s$
.
print "Show all solutions that sum to 100\n"
for i = 0 to nexpr
   if f[i] = 100
      out i
   .
.
print "\nShow the sum that has the maximum number of solutions\n"
for i = 0 to nexpr
   test = f[i]
   if test > 0
      ntest = 0
      for j = 0 to nexpr
         if f[j] = test
            ntest += 1
         .
      .
      if ntest > nbest
         best = test
         nbest = ntest
      .
   .
.
print best & " has " & nbest & " solutions"
print "\nShow the lowest positive number that can't be expressed\n"
for i = 0 to 123456789
   for j = 0 to nexpr
      if i = f[j]
         break 1
      .
   .
   if j > nexpr
      break 1
   .
.
print i
print "\nShow the ten highest numbers that can be expressed\n"
limit = 123456789 + 1
for i = 1 to 10
   best = 0
   for j = 0 to nexpr
      test = f[j]
      if test < limit and test > best
         best = test
      .
   .
   for j = 0 to nexpr
      if f[j] = best
         out j
      .
   .
   limit = best
.
