Created
March 27, 2016 00:26
-
-
Save monkeygroover/c05d1dc0da7989bba377 to your computer and use it in GitHub Desktop.
euler21
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 factors number = seq { | |
for divisor in 1 .. (float >> sqrt >> int) number do | |
if number % divisor = 0 then | |
yield divisor | |
if number <> 1 then yield number / divisor //special case condition: when number=1 then divisor=(number/divisor), so don't repeat it | |
} | |
let d n = Seq.sum(factors n) - n | |
let all = {1..9999} |> Seq.map(fun a -> (a, d(a))) |> Map.ofSeq | |
let amicable (all: Map<int, int>) = Seq.filter(fun (a,b) -> a<>b && all.TryFind(b) = Some a) (Map.toSeq(all)) | |
let result = all |> amicable |> Seq.map(fun (a,b) -> a) |> Seq.sum | |
printfn "%A" result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment