Project Euler – Problem 32 Solution

You can become a serverless blackbelt. Enrol in my course Learn you some Lambda best practice for great good! and learn best practices for performance, cost, security, resilience, observability and scalability. By the end of this course, you should be able to make informed decisions on which AWS service to use with Lambda and how to build highly scalable, resilient and cost efficient serverless applications.

Problem

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

Solution

let isPandigital(n) =
    let upperBound = int32(sqrt(double(n)))

    [2..upperBound]
    |> Seq.filter (fun x -> n % x = 0)
    |> Seq.map (fun x -> x.ToString()+(n/x).ToString()+n.ToString())
    |> Seq.exists (fun str -> 
        [1..9]
        |> List.map (fun n -> n.ToString())
        |> List.forall (fun n -> str.Contains(n) && str.IndexOf(n) = str.LastIndexOf(n)))

let answer = [1000..9999] |> List.filter isPandigital |> List.sum

This problem here is somewhat open ended in terms of how far you have to go before you can be sure that you’ve checked all the relevant numbers, i.e. do I have to cover all 1-5 digits numbers, or 1-9, or somewhere in between?

To answer that, think of each of multiplicand, multiplier and product as a * (10 POW b) – e.g. 39 = 0.39 * (10 POW 2), 186 = 0.186 * (10 POW 3) and 7254 = 0.7254 * (10 POW 4) – then you end up with:

a1 * (10 POW b1) * a2 * (10 POW b2) = a3 * (10 POW b3) where b1 + b2 + b3 = 9 and b1 + b2 >= b3

As it turns out the only b3 which satisfies both criteria is 4, so we only need to check 4-digit products, hence you see in my solution that I’ve only considered the numbers from 1000 to 9999.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.


Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.


Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!


Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!


Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you