Created
August 22, 2011 17:35
-
-
Save davidgrenier/1162987 to your computer and use it in GitHub Desktop.
Project Euler 5
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 getPrimeFactors n = | |
| let allFactors = seq { yield 2; yield! Seq.initInfinite (fun i -> 2 * i + 3) } |> Seq.cache | |
| let addFactor factors d = | |
| match Map.tryFind d factors with | |
| | None -> Map.add d 1 factors | |
| | Some count -> Map.remove d factors |> Map.add d (count + 1) | |
| let rec gPF n factors = | |
| allFactors | |
| |> Seq.takeWhile (fun d -> d <= (n |> float |> sqrt |> int)) | |
| |> Seq.tryFind (fun d -> n % d = 0) | |
| |> function | |
| | None -> addFactor factors n | |
| | Some d -> addFactor factors d |> gPF (n / d) | |
| gPF n Map.empty;; | |
| Seq.init 20 ((+) 1) | |
| |> Seq.map getPrimeFactors | |
| |> Seq.concat | |
| |> Seq.map (|KeyValue|) | |
| |> Seq.groupBy fst | |
| |> Seq.map (fun (f, counts) -> pown f (counts |> Seq.map snd |> Seq.max)) | |
| |> Seq.reduce (*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment