Project Euler – Problem 21 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

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.


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


2 thoughts on “Project Euler – Problem 21 Solution”

  1. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *