P99
projecteuler.net

Largest Exponential

ℹ️Published on Friday, 1st July 2005, 06:00 pm; Solved by 33633;
Difficulty rating: 10%

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 \lt 3^7 = 2187$.

However, confirming that $632382^{518061} \gt 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.



Soluzione

Semplicemente apriamo il file e anziché calcolare espressamente le potenze \(a^b\) calcoliamo \(b\cdot \log(a)\) in modo da agevolare il confronto lavorando con numeri più piccoli.

file = download("https://projecteuler.net/resources/documents/0099_base_exp.txt")
max_line = 1
max_val = 1
curr_line = 0
for line in eachline(file)
    global curr_line += 1
    numbers = split(line, ",")
    a = parse(Int, numbers[1])
    b = parse(Int, numbers[2])
    val = b*log(a)
    if val>max_val
        global max_val = val
        global max_line = curr_line
    end
end
print("max line = $max_line with value $max_val")
max line = 709 with value 6.919995552420337e6

Last modified: August 18, 2025. Website built with Franklin.jl and the lovely Julia programming language.