Project Euler – Problem 30 Solution

Yan Cui

I help clients go faster for less using serverless technologies.


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.


// get the digits of a number into an array
let getDigits (n:bigint) = n.ToString().ToCharArray() |> (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) |> (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

// get the next number with the given number of digits
let maxNumber digits = [1..digits] |> (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:


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:


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.


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.


1 thought on “Project Euler – Problem 30 Solution”

  1. Pingback: Project Euler — Problem 34 Solution |

Leave a Comment

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