# Project Euler Problem 21

Solving Project Euler Problem 21 would require introducing a function **divisorSum** yielding for any natural number **n** a sum of its divisors. Equipped with this function we can define a function **isAmicable** that detects amicability of any natural number **n**. With this function we simply filter out all non-amicable numbers from the interval and sum amicable ones:

let divisorSum n = let divisors n = [ yield 1 for i in 2..int (sqrt(float n)) do if n%i = 0 then yield i if i <> n/i then yield n/i ] divisors n |> List.sum let isAmicable a = let pair = divisorSum a a <> pair && a = divisorSum pair let problem021 () = {1..9999} |> Seq.filter isAmicable |> Seq.sum

Advertisements

## Trackbacks & Pingbacks