# Euler Problem 29: Distinct Powers

Euler Problem 29 is another permutation problem that is quite easy to solve using brute force. The MathBlog site by Kristian Edlund has a nice solution using only pen and paper.

Raising a number to a power can have interesting results. The video below explains why this pandigital formula approximates to billions of decimals:

## Euler Problem 29 Definition

Consider all integer combinations of: for and .

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

How many distinct terms are in the sequence generated by for and ?

## Brute Force Solution

This code calculates all powers from to and determines the number of unique values. Since we are only interested in their uniqueness and not the precise value, there is no need to use Multiple Precision Arithmetic.

View the code below or download latest version of this code on GitHub.

target <- 100 terms <- vector() i <- 1 for (a in 2:target) { for (b in 2:target) { terms[i] <- a^b i <- i + 1 } } answer <- length(unique(terms)) print(answer)

[…] article was first published on The Devil is in the Data, and kindly contributed to […]

Ack! No vectorization and growing an object in a loop! You know 99 * 99 elements, so initialize it to that length! And then

`^`

is vectorized so you only need one for loop,`will calculate 99 terms at once. Or skip the loops entirely and use this:`

Hi Gregor,

Thanks for the lesson, I did not know the outer command.

Your one-line solution is much faster 🙂

Peter

You’re quite welcome 🙂

Really though, avoid growing objects in loops. Compare:

[code language=”r”]

n = 1e7

x1 = numeric()

for (i in 1:n) x1[i] <- 1

x2 = numeric(n)

for(i in 1:n) x2[i] <- 1

[/code]

Initializing the object to the correct length is *much* faster than extending its length every time. With a vector, it’s pretty quick either way, but with a data frame the difference is huge:

[code language=”r”]

ndf = 3e4

d1 = data.frame(x = numeric())

for (i in 1:ndf) d1 = rbind(d1, data.frame(x = 1))

d2 = data.frame(x = numeric(ndf))

for (i in 1:ndf) d2$x[i] <- 1

[/code]

Just form the good habit of always pre-allocating and you’ll avoid a common, needless bottleneck.

[…] article was first published on The Devil is in the Data, and kindly contributed to […]

[…] numbers to the power of five. Two other Euler problems dealt with raising numbers to a power. The previous problem looked at permutations of powers and problem 16 asks for the sum of the digits […]

[…] numbers to the power of five. Two other Euler problems dealt with raising numbers to a power. The previous problem looked at permutations of powers and problem 16 asks for the sum of the digits of […]

[…] Problem 32 returns to pandigital numbers, which are numbers that contain one of each digit. Like so many of the Euler Problems, these […]

[…] Problem 32 returns to pandigital numbers, which are numbers that contain one of each digit. Like so many of the Euler Problems, these […]