Skip to content

Instantly share code, notes, and snippets.

@jarlestabell
Created May 15, 2015 20:18
Show Gist options
  • Save jarlestabell/88db6f13d441f79f6fa2 to your computer and use it in GitHub Desktop.
Save jarlestabell/88db6f13d441f79f6fa2 to your computer and use it in GitHub Desktop.
This one is quite fast. (Except for the printing, it runs in less than a microsecond)
let calcExpr digitCount n=
let rec innerCalc n current=
if current>digitCount then "" else [|" - "; ""; " + "|].[n%3] + string current + innerCalc (n/3) (current+1)
"1" + innerCalc n 2
let eval digitCount n=
let rec innerCalc n current (partialsum, partialnumber)=
if current>digitCount then partialsum + partialnumber
else
let nextPartials = if n % 3 = 1 then (partialsum, partialnumber * 10 + (current * (sign partialnumber)))
else (partialsum + partialnumber, current * sign ((n % 3) - 1))
innerCalc (n/3) (current+1) nextPartials
innerCalc n 2 (0, 1)
let solutions digitCount = seq {0 .. (pown 3 (digitCount - 1)) - 1} |> Seq.filter(fun n->eval digitCount n = 100)
let generateAndPrint() = solutions 9 |> Seq.map (calcExpr 9) |> Seq.iter (printfn "%s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment