Task

Find solutions to the    sum to one hundred    puzzle.



Add (insert) the mathematical
operators      +    or    -      (plus
or minus)   before any of the digits in the

decimal numeric string    123456789    such that the
resulting mathematical expression adds up to a

particular sum   (in this iconic case,   100).



Example:

             123 + 4 - 5 + 67 - 89   =   100     

Show all output here.



   Show all solutions that sum to    100 
   Show the sum that has the maximum   number   of solutions   (from zero to infinity‡)
   Show the lowest positive sum that   can't   be expressed   (has no solutions),   using the rules for this task
   Show the ten highest numbers that can be expressed using the rules for this task   (extra credit)


‡   (where   infinity   would be a relatively small   123,456,789)



An example of a sum that can't be expressed   (within the rules of this task)   is:   5074

(which,   of course,   isn't the lowest positive sum that can't be expressed).




import algorithm, parseutils, sequtils, strutils, tables
 
type Expression = string
 
proc buildExprs(start: Natural = 0): seq[Expression] =
  let item = if start == 0: "" else: $start
  if start == 9: return @[item]
  for expr in buildExprs(start + 1):
    result.add item & expr
    result.add item & '-' & expr
    if start != 0: result.add item & '+' & expr
 
proc evaluate(expr: Expression): int =
  var idx = 0
  var val: int
  while idx < expr.len:
    let n = expr.parseInt(val, idx)
    inc idx, n
    result += val
 
let exprs = buildExprs()
var counts: CountTable[int]
 
echo "The solutions for 100 are:"
for expr in exprs:
  let sum = evaluate(expr)
  if sum == 100: echo expr
  if sum > 0: counts.inc(sum)
 
let (n, count) = counts.largest()
echo "\nThe maximum count of positive solutions is $1 for number $2.".format(count, n)
 
var s = 1
while true:
  if s notin counts:
    echo "\nThe smallest number than cannot be expressed is: $1.".format(s)
    break
  inc s
 
echo "\nThe ten highest numbers than can be expressed are:"
let numbers = sorted(toSeq(counts.keys), Descending)
echo numbers[0..9].join(", ")