Created
May 16, 2015 14:07
-
-
Save einarwh/865942e9139aba14b66c to your computer and use it in GitHub Desktop.
1-9 to 100 directly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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