Project Euler – Problem 30 Solution

You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!

Problem

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44

8208 = 84 + 24 + 04 + 84

9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Solution

// get the digits of a number into an array
let getDigits (n:bigint) = n.ToString().ToCharArray() |> Array.map (fun c -> bigint.Parse(c.ToString()))

// get the sum of a number's digits to the specified power
let getDigitsToPowerSum (n:bigint) pow = 
    getDigits(n) |> Array.map (fun x -> pown x pow) |> Array.sum

// get the max sum achievable by a number of the given number of digits to the specified power
let upperBound (digits:int) pow = bigint(digits) * pown 9I pow

// find the last number of digits n, where the max sum achievable by the digits of a number of
// n digits to the specified power is greater than the smallest number of n digits
// any number with more than n digits do not need to be checked
let digitsToCheck pow =
    let n =
        Seq.unfold (fun state -> Some(state, (state+1))) 1
        |> Seq.filter (fun x -> (upperBound x pow).ToString().ToCharArray().Length < x)
        |> Seq.head
    n-1

// get the next number with the given number of digits
let maxNumber digits = [1..digits] |> List.map (fun x -> 9I * pown 10I (x-1)) |> List.sum

let answer =
    let max = maxNumber (digitsToCheck 5)
    [2I..max] |> List.filter (fun n -> n = getDigitsToPowerSum n 5) |> List.sum

One of the tricky things with this problem is that there’s no upper limit to how far you’d need to test before you can safely determine that you’ve tested all the numbers whose sum of fifth powers of their digits MIGHT be equal to the numbers themselves.

But that’s not to say it’s not possible, for a 1-digit number, i.e. 1..9, the max sum of its digits is 1 * (9 POW 5) = 59049; similarly for a 2-digit number the max sum is 2 * (9 POW 5) = 118098. The upperBound function calculates this upper ceiling for a number of a given digits and power.

Let’s take a closer look at the distribution of this upper limit as the number of digits go up, for the fourth power:

image

so as you can see, when the number of digits reaches 6, the max sum of fourth powers of digits is 39366, but the smallest 6-digit number is 100000! Therefore it’s impossible for any 6 digit number to be written as the sum of fourth powers of its digits, and it means we’ll need to go as far as covering all 5 digit numbers. The digitsToCheck function simply encapsulates this bit of logic:

image

The maxNumber function is a helper function which generates the biggest number of the given number of digits. To find the answer, I simply had to make use of all the helper functions mentioned so far and sum the numbers whose fifth powers of its digits equals itself.

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 I help companies go faster for less by adopting serverless technologies successfully.

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.


Skill up your serverless game with this hands-on workshop.

My 4-week Production-Ready Serverless online workshop is back!

This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you hands-on experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.

We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.

If you enrol now you can also get 15% OFF with the promo code “yanprs15”.

Enrol now and SAVE 15%.


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!