Skip to content

Project Euler Problem 21

November 16, 2011

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

From → Project Euler

One Comment

Trackbacks & Pingbacks

  1. Project Euler Problem 23 « In F# Major

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: