Project Euler – Problem 21 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

The real-time data platform that empowers developers to build innovative products faster and more reliably than ever before.

Learn more

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. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

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 *