Created
August 19, 2011 15:31
-
-
Save davidgrenier/1157084 to your computer and use it in GitHub Desktop.
Project Euler 4
This file contains hidden or 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 generate a b = seq { | |
yield (a, b) | |
let rec down (a, b) diff secondPass = seq { | |
let next = a - diff, b + diff - 1 | |
yield next | |
yield! up next diff (not secondPass) | |
} | |
and up (a,b) diff secondPass = seq { | |
yield! Seq.init diff (fun i -> a + 1 + i, b - 1 - i) | |
yield! down (a + diff, b - diff) (if secondPass then diff + 1 else diff) secondPass | |
} | |
yield! down (a, b) 0 false | |
};; | |
generate 999 999 | |
|> Seq.map (fun (a,b) -> a * b) | |
|> Seq.find (fun v -> let s = string v in new string(s.ToCharArray() |> Array.rev) = s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment