Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for ProjectEuler Problem 99

Tags:

algorithm

math

From http://projecteuler.net/index.php?section=problems&id=99

Comparing two numbers written in index form like 211 and 37 is not difficult, as any calculator would confirm that 211 = 2048 37 = 2187.

However, confirming that 632382518061 > 519432525806 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.

How might I approach this?

like image 852
Devoted Avatar asked Jan 13 '09 10:01

Devoted


2 Answers

Not a full solution, but some ideas. You can use the following formula:

log(ax) = x*loga

The log10 can easily be estimated as number of digits. The log2 can easily be estimated by counting right shifts.

Based on the above you could narrow the list significantly. For the remaining numbers, you would have to do full calculations. Are math functions allowed in project Euler? If yes, it would be better to use logarithms.

like image 145
kgiannakakis Avatar answered Oct 03 '22 10:10

kgiannakakis


Since the logarithm is a monotonic function, instead of ax you could compare x * log a to find the maximum. You might need to take numerical precision into account, though.

like image 30
starblue Avatar answered Oct 03 '22 09:10

starblue