// you’re reading...

Project Euler Solutions

Project Euler Problem 99 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

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.

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

Analysis

Very simple to compare the product of the exponent by the log of the base and keep the maximum value and respective line number.

Solution

Runs < 1 second in Python.

from math import log
 
mv, ml, ln = 0, 0, 0 
for line in file('base_exp.txt'): 
  ln += 1
  b, e = line.split(',') 
  v = int(e) * log(int(b)) 
  if v > mv: 
    mv, ml = v, ln 
print "Answer to PE99 = ", ml

Comments

Discussion

No comments for “Project Euler Problem 99 Solution”

Post a comment