Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created May 16, 2015 14:07
Show Gist options
  • Save einarwh/865942e9139aba14b66c to your computer and use it in GitHub Desktop.
Save einarwh/865942e9139aba14b66c to your computer and use it in GitHub Desktop.
1-9 to 100 directly
let solve lim total =
let rec gen (n::t) s =
let m = (n % 10) % lim
if m = 0 then
if n = lim then [(List.head (List.rev (n::t)), s)] else []
else
match t with
| [] ->
gen ((m+1)::n::t) (string n) @
gen ((10 * n + m + 1)::t) s
| sum::r ->
gen ((m+1)::(sum + n)::r) (s + "+" + string n) @
gen ((m+1)::(sum - n)::r) (s + "-" + string n) @
gen ((10 * n + m + 1)::t) s
gen [1] "" |> List.filter (fun (sum, s) -> sum = total) |> List.map (fun (sum, s) -> s)
solve 10 100 |> printfn "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment