## Project Euler Problem #119

### April 10, 2010

Read the details of the problem here

Summary

Investigating the numbers which are equal to sum of their digits raised to some power.

Solution

My first attempt at this was a simple brute force approach to try to find the numbers in order. I was dividing the log of the number by the log of the sum of the digits to get the power and then checking to see if this was an integer. This was working in principle but was obviously not going to scale up to finding the 30th term .

The next approach, which was successful, was to generate a set of base numbers raised to a range of powers and check them for matching the criteria, adding them into a result list when they did. In this way I was not certain of generating the numbers in order so have to search for more than specified then sort & select the required element.

```def results = []

for (base in 2..200) {
for (pow in 2..20) {
def n = (base ** pow).toBigInteger()
def sum = n.toString().toList().sum { it.toInteger() }
if (sum == base) results << [ n, base, pow ]
if (results.size() == 60) break
}
}

results.sort { it[0] }