#### Problem

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

1634 = 1^{4}+ 6^{4}+ 3^{4}+ 4^{4}

8208 = 8^{4}+ 2^{4}+ 0^{4}+ 8^{4}

9474 = 9^{4}+ 4^{4}+ 7^{4}+ 4^{4}

As 1 = 1^{4}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:

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.

Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.

**Yan Cui**

I’m an **AWS Serverless Hero** and the author of **Production-Ready Serverless**. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

Check out my new 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. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.

Further reading

Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.

- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong