I thought of two solutions for Problem 99 of Project Euler. One which I knew would be reasonably fast and the other one which I was curious about. The latter one is completely intractable though. The problem reads

Comparing two numbers written in index form like 2^{11}and 3^{7}is not difficult, as any calculator would confirm that 2^{11}= 2048 < 3^{7}= 2187.

However, confirming that 632382^{518061}> 519432^{525806}would be much more difficult, as both numbers contain over three million digits.

Using base_exp.txt (right click and ‘Save Link/Target As…’), a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value.

NOTE: The first two lines in the file represent the numbers in the example given above.

At first I thought it would be fun to see how the BigInteger class would fare in this problem. The answer is not so well. In fact calculating the first number alone takes 167s which is already above the 1 minute allowed limit for the problems, let alone calculate all thousands of the numbers.

The second approach I took was using logarithms. One of the rules of logarithms is the power rule which states

So instead of calculating the power, we can take a logarithm of any base and convert the problem to a multiplication problem. Which means that the problem has been reduced to taking the logarithm and multiply that by a number.

The code for that can be written in C# as

int maxline = 0; double maxnum = 0; string[] lines = File.ReadAllLines(filename); for (int i = 0; i < lines.Length; i++) { string[] line = lines[i].Split(','); double basenum = Math.Log(Convert.ToInt32(line[0])); int exponent = Convert.ToInt32(line[1]); double number = basenum * exponent; if (number > maxnum) { maxline = i+1; maxnum = number; } }

This code, unlike the first appraoch runs in

The line with the largest number is 709 Solution took 1,3466 ms

## Wrapping up

Only one more problem to go till we reach 100 and even though this particular problem was actually rather easy. The source code for problem 99 can be found here.

Did you solve it another way?

A similar but much harder problem is as follows:

Given an integer and two integer sequences and where , determine which of the expressions and has the greatest numerical value.

Source: ALGORITHM PROBLEMS FOR DUMMIES: PETR MITRICHEV’S BLOG.

I guess that the challenge here lies within the precision of the solution. At least according to the blog. Because if you had infinite precision you should be able to use the same approach as in this problem.

Precision may or may not be the problem, but at least the approach we’re using here won’t work.

You may by thinking about doing it like this, but it’s easy to see that this is wrong. You can try deriving it, but after a few steps you encounter which he wrongly assumes is .

I still haven’t found a valid solution. Petr thought he had a valid solution, but he still hasn’t published it (he may have realized it was wrong).

More discussion can be found here.

No it wasn’t based on that, and yes I can see that he makes a wrong assumption there.

I just thought that you could apply the log repeatedly such that if you have you could end up with and then my logic started to fail.

However, I think that we might make this overly complicated since . I know you will end up with a potentially very large factor, but you could rather easily make a prime factorization of it (due to all components being factored into divisors less than 100). Then you can start weeding out common factors of the two exponents. Since you can make where c’ is the gcd of the exponents on both sided.

After that you “just” need to figure out which of is bigger. I am uncertain whether this helps or not.

Many people get this wrong, and I probably should have stated it explicitly.

is right associative. Computing is the same as computing (because ).

So is not .

But if we were evaluating the tower like you thought, your method would have been correct. But that problem is a lot easier than this one.

(It’s also funny how you get the order right when you’re taking the logarithm, but get it wrong in the next paragraph.)

Yes I see that now… I should have thought that a bit more through before talking. Anyway interesting problem which I need to follow the discussion for.

Did you confirm this solution?

Comparing 1^100,2^99, 3^97… with 1*100,2*99,3*98… I find that 24^76 is way bigger than 50^50, while 50*50 is way bigger than 24*76. They have completely different maxima, so simply multiplying the results does not seem to work. Should’n in the line “double number = basenum * exponent;” be an exponent invoked?

Thanks!

Zsolt: read the source again. He is not multiplying doing 50 * 50, but Math.Log(50) * 50 and Math.Log(24) * 76.