Skip to content

Instantly share code, notes, and snippets.

@monkeygroover
Created March 27, 2016 00:26
Show Gist options
  • Save monkeygroover/c05d1dc0da7989bba377 to your computer and use it in GitHub Desktop.
Save monkeygroover/c05d1dc0da7989bba377 to your computer and use it in GitHub Desktop.
euler21
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