Project Euler – Problem 21 Solution

Problem

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).

If d(a) = b and d(b) = a, where a != b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

Solution

open System

let findDivisors(n) =
    let upperBound = int32(Math.Sqrt(double(n)))

    [1..upperBound]
    |> Seq.filter (fun x -> n % x = 0)
    |> Seq.collect (fun x -> [x; n/x])
    |> Seq.filter (fun x -> x <> n)

let d(n) = findDivisors(n) |> Seq.sum
let dList = [ for n = 1 to 9999 do yield (n, d(n)) ]

let answer =
    dList
    |> List.filter (fun (a, da) -> dList
                                   |> List.exists (fun (b, db) -> b = da && a = db && a <> b))
    |> List.sumBy (fun (n, dn) -> n)

First I defined a findDivisors function which returns all the divisors of a given number excluding itself as is the case in the example given in the brief. Then I defined the function d which returns the sum of the given number’s proper divisors.

To get to the answer, I generated a list of tuples for the numbers from 1 to 9999 in the form of (n, d(n)) and from there I iterated through the list and for each tuple check whether there exists another tuple which matches the conditions required for the two to be considered an amicable pair.

You might have noticed in the function applied to List.filter to identify amicable pairs that I have decomposed the tuple into (a, da), as I have mentioned before this is a form of the pattern matching in F#. The same pattern matching technique is also used when I calculate the sum of the amicable pairs.

  • Euler

    Surprising how there are so many different approaches to solving a problem. I defined the d function and then I checked if n = d ( d n && n d(n) ). Applying the function twice should map a number back to itself but 1 and 6, for example, equal themselves after the applying the function d so have to be excluded as answers.

  • Yan Cui

    nice! :-)