sumTo100.frink

Download or view sumTo100.frink in plain text format


/** This is a solver for the Rosetta Code puzzle "Sum to 100"
    http://rosettacode.org/wiki/Sum_to_100
*/

digits = array[1 to 9]
opList = makeArray[[8], ["", " + ", " - "]]
opList.pushFirst[["", "-"]]
countDict = new dict

multifor ops = opList
{
   str = ""
   for d = rangeOf[digits]
      str = str + ops@d + digits@d
   e = eval[str]
   countDict.increment[e, 1]
   if e == 100
      println[str]
}
println[]

// Find the sum that has the maximum number of solutions
freq = toArray[countDict]
sort[freq, {|a,b| -(a@1 <=> b@1)}]
max = freq@0@1
print["Maximum count is $max at: "]
n = 0
while freq@n@1 == max
{
   print[freq@n@0 + " "]
   n = n + 1
}
println[]

// Find the smallest non-representable positive sum
sort[freq, byColumn[0]]
last = 0
for [num, count] = freq
{
   if num > 0 and last+1 != num
   {
      println["Lowest non-representable positive sum is " + (last+1)]
      break
   }
   last = num
}

// Find highest 10 representable numbers
println["\nHighest representable numbers:"]
for [num, count] = last[freq, 10]
   println[num]


Download or view sumTo100.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19944 days, 8 hours, 59 minutes ago.