Problem 119 of Project Euler reads

The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 8^{3}= 512. Another example of a number with this property is 614656 = 28^{4}.

We shall definea_{n}to be thenth term of this sequence and insist that a number must contain at least two digits to have a sum.

You are given thata_{2}= 512 anda_{10}= 614656.

Finda_{30}.

To be honest I have a bit of a bad feeling about the solution for this as it is based a whole lot on heuristics and guess when choosing the parameters. But lets give it a spin and see where we end up.

I saw two approaches for this. Either we go through each number and see if their digit sum can be raised to a power such that it is equal to the number. Or we go the other way around and see raise smaller numbers to different powers and check their digit sum. I had a feeling that the number we are looking for here is pretty large. So taking the first approach would probably be infeasible due to the time of computation. So I took the other route.

We have already been over the code idea, so let us just go through it

List<BigInteger> a = new List<BigInteger>(); for (int b = 2; b < 400; b++) { BigInteger value = b; for (int e = 2; e < 50; e++) { value *= b; if (DigitSum(value) == b) { a.Add(value); } if (a.Count > 50) break; } if (a.Count > 50) break; }

You can see now that I have chosen the limits for the base and exponent at random. Furthermore I have decided to find the first 50 numbers. The reason I decided to go above 30 is the fact that we cannot be sure to find them in the right order.

Just take the first three we find which are 7^{4}= 2401, 8^{3}= 512, 9^{2}= 81 which are a_{3}, a_{2} and a_{1}.

## Wrapping up

In order to run this code we need the DigitSum method, which you can find in the source code. Running this piece of code can be done in just under 60ms. so it is pretty fast. And we will get the result

a30 is 248155780267521

I really wanted to provide a better solution for this one, but I had to give up. If we go the way with generating power we need to run into the billions squared in order to ensure that nothing is smaller than the actual solution.

So if you have a better solution for this, please let me know.

i gotta give it to ya man for sticking with it for so long. I need to get back on track but I got people tellin me to learn F# not C#. What;s your thoughts on this?

Hi, I have pretty much the same solution for this problem, just different limits.

Hi Billy

Thanks first of all, the thing that has got me going for so long is this website. The feedback I get is what have kept the spirit up. The answer the question whether to learn C# or F# is one of those “it depends” answers. I don’t think you can really that one is better than the other. They are different languages with different strengths. Learning both will give you a whole lot of insights in a lot of things.

I can advice you to listen to .net rocks episode 803 there they discuss speaking multiple languages.

/Kristian

Hi Eman

As I mentioned, the limits were chosen more or less randomly. So in that regard I am not really surprised yours a different. Otherwise nice solution.

Pretty much same 😀

Project Euler Problem 119 was posted on April 7, 2006.

http://projecteuler.net/problem=119

Thanks to Kristian for this impressive C# solution of this problem.

Some results calculated with Kristian’s algorithm.

a1=81 (9^2)

a2=512 (8^3)

a3=2401 (7^4)

a4=4913 (17^3)

a5=5832 (18^3)

a6=17576 (26^3)

a7=19683 (27^3)

a8=234256 (22^4)

a9=390625 (25^5)

a10=614656 (28^4)

a20=24794911296 (54^6)

a30=248155780267521 (63^8)

Hi Kristian,

Just 2 points concerning Project Euler Problem 119.

______

Point 1 :

Adding these 2 lines :

Console.WriteLine(“Problem119”);

Console.ReadLine();

After this one :

Console.WriteLine(“Solution took {0} ms”, clock.Elapsed.TotalMilliseconds);

Will give access to the results for a period of time as long as required by the user.

(Without this change, the results appear just a few ms on my computer).

___

Point 2 :

Actual line :

int[] perm = new int[] {1,2,3,4,5,6,7,8,9};

This simplified line gives the same results :

int[] perm = new int[] {1};

______

Thanks again for this code.

More comments to come on the results.

Jean-Marie

Regarding point 1. On my computer the console stays up after the program has run. Which is why I never bothered to add something like that. Otherwise I would have to press the keyboard twice.

Regarding point 2. It is because the variable is never used. It is a leftover from a previous problem. So the correct solution is to delete it all together. That is done now in the downloadable source code.

I have an idea about the limits. Let us assume that our base is x and the exponent is n. I had solved this on Hackerrank and they specifically mentioned the upper limit of x^n to be 10^100.

So I worked with the constraints 10 < x^n < 10^100. If I knew the range of x, I could solve for n's range using logarithms. So I saw that the max value of sum of digits of x^n could be 9+9+….9 (99 times) = 891 (comes from upper limit 10^100). Since we are looking for a solution where (sum of digits of x^n) = x, and maximum sum of digits was 891, therefore x must be lesser than or equal to 891. I finally took the range of x as 2<=x<=891, and calculated n as:

1/log x <= n <= 100/log x (logarithms have a base of 10).

It worked for me, so I am guessing this method was correct, but you need to know the upper limit or take a guess.

You can generate numbers a^b in order by keeping track of “potential” next numbers in a priority queue, sorted in increasing order. Start with 2^2=4 in the queue, and the next smallest exponent k=3. The next number is either the top of the queue, or 2^k. If the top of the queue is smaller, check if it satisfies the digit-sum condition, and add (a+1)^b to the queue. Otherwise, add 3^k to the queue, and set k:=(k+1).

This is much slower than your provided solution, since it constantly adds/removes from a priority queue. However, you are guaranteed to find the sequence in order, so can terminate as soon as you find 30 elements.

A better way to guarantee you don’t miss any numbers is to consider c=a^b in increasing number of digits of c. Start with k=2 digit numbers, and increase k until you have enough numbers in your sequence. You know the maximum base a is the maximum sum of k digits = 9k. The minimum base is always 2. The minimum exponent to generate a k-digit number is (k-1)/log10(a), and you keep increasing exponents until you have formed a k+1 digit number. Finally, you need to sort your sequence.

I am getting the answer here. But the thing is the answer 248155780267521 does not lie in the right order.

Here is my code. As I am a complete beginner , please ignore my style of coding.

using System;

class DigitPowerSum1

{

public static ulong[] nums= new ulong[30];

public ulong[] DigPowSum()

{

ulong i, count = 0, powexp = 0, sum, j, p, ind = 0, a;

ulong[] spd = new ulong[30];

char[] c;

string s = “”;

for (i = 2; i <= 400 && count < 60; i++)

{

for (j = 2; j <= 200; j++)

{

powexp = (ulong)(Math.Pow(i, j));

s = powexp + "";

c = s.ToCharArray();

sum = 0;

for (int k = 0; k 0)

{

if (sum == i)

{

spd[ind] = i;

nums[ind] = powexp;

ind++;

count++;

}

}

if (count == 30)

{

break;

}

}

}

return spd;

}

}

class Test

{

public static void Main()

{

DigitPowerSum1 obj = new DigitPowerSum1();

ulong[] spd = obj.DigPowSum();

ArraySorting obj1 = new ArraySorting();

obj1.ArraySort(DigitPowerSum1.nums);

obj1.ArraySort(spd);

Console.WriteLine(spd[29]);

Console.WriteLine(DigitPowerSum1.nums[29]);

Console.WriteLine();

for (int i = 0;i<= DigitPowerSum1.nums.Length-1;i++)

{

Console.WriteLine(DigitPowerSum1.nums[i]);

}

Console.WriteLine();

for (int i = 0; i <= spd.Length – 1; i++)

{

Console.WriteLine(spd[i]);

}

}

}

I have a solution and only takes about 135 us on my MBPR 15″ using Golang. Since highlight of Golang is not supported in this comment, please go to my github page.

https://github.com/EvenPeng/Project-Euler-using-Golang/blob/master/p119.go

I use a integer array to store the power of each base and base is its index. Since the power of 2 will always be the minimum values, I first update the power of 2 first in each round. Then, append the possible bases to the end of the array (line 46 – 52). Finally, update the array from 3 to the end of the array (line 54 – 63). The sequence is generated in ordered list.

Hey guys. It’s cool all of the codes Yoirr creating this sequence, but I discovered a larger 2-parameter (m,n) generalization of this phenomena.

For example, for (m=3,n=3), we have the example (a1+a2+am)^n=(5+1+2)^3=512=5

10^2+110^1+2*10^0Another example for the parameter (5,3) is (1+9+6+8+3)^3=19683.

The question now is concerning whether there will always be solutions as n, the power increases, or whether I have a certain point the number of search solutions terminates, or we simply have powers for which a solution does not exist. Email me camimont@indiana.edu if you’re interested in discussing this problem, such as this way which I’m calling the polynomial p(10)=[p(1)]^m approach.

(1+9+8+6+3)^3= 19863

It is also possible to generate all powers of all numbers in order. Start with #{2 = 2^1}. Assume #{…} is a sorted set. Take the minimum out of the set which is 2 and increase the exponent by 1. It becomes 4 = 2^2. We also ensure at least one element with exponent 1 is maintained in the set. So we insert 3 = 3^1 in the set as well. We will know when to enter a new base in the set by checking the exponent of the just removed member.

So the sorted set grows like this.

#{2 = 2^1}

#{3 = 3^1, 4 = 2^2}

#{4 = 2^2, 5 = 5^1, 9 = 3^2}

#{5 = 5^1, 8 = 2^3, 9 = 3^2}

… and so on.

Run this sequence of powers through a filter (= (digital-sum n) b). This way, the numbers are guaranteed to be in order.