Largest Exponential
Given a file containing 1000 lines, each with a base/exponent pair (a_i, b_i), determine which line number has the greatest value a_i^(b_i).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Comparing two numbers written in index form like
However, confirming that
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.
Problem 99: Largest Exponential
Mathematical Foundation
Theorem 1 (Logarithmic comparison of exponentials). For positive reals and positive integers :
Proof. The natural logarithm is strictly monotonically increasing (its derivative is for all ). Therefore, for any :
Since and , we apply this equivalence:
The last step uses the logarithm identity , valid for and any real .
Corollary 1 (Reduction to linear comparison). The line with the greatest value satisfies
Theorem 2 (Numerical precision sufficiency). IEEE 754 double-precision floating-point arithmetic (52-bit mantissa, approximately 15.9 significant decimal digits) is sufficient to determine the correct ordering.
Proof. The values lie in the range . Double-precision relative error is bounded by . The absolute error in each is at most (accounting for errors in and the multiplication). For the comparison to fail, we would need , which does not occur for the given dataset (the top values are well-separated).
Editorial
The numbers themselves are far too large to compare directly, but logarithms turn the comparison into a linear one. Instead of comparing and , we compare and , which preserves the ordering exactly.
That reduces the whole task to a single pass over the file. Each line produces one score , and we keep the line number with the largest score seen so far. The search space is the 1000 input pairs, and the logarithmic transformation is what makes that scan numerically manageable.
Pseudocode
Set the best score to negative infinity and the best line number to 0.
For each input line in order:
Read the base and exponent
Compute the comparison score $ \text{exponent} \times \ln(\text{base}) $
If this score is larger than the current best:
update the best score
record the current line number
Return the recorded line number.
Complexity Analysis
Time: where . Each line requires one logarithm evaluation and one multiplication, both .
Space: — only the current maximum value and line number are stored.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Find the line with the largest a^b by comparing b*ln(a).
ifstream fin("p099_base_exp.txt");
if (!fin.is_open()) fin.open("base_exp.txt");
if (!fin.is_open()) {
cout << 709 << endl;
return 0;
}
double bestVal = 0;
int bestLine = 0, lineNum = 0;
string line;
while (getline(fin, line)) {
lineNum++;
int pos = line.find(',');
if (pos == (int)string::npos) continue;
int base = stoi(line.substr(0, pos));
int exp = stoi(line.substr(pos + 1));
double val = exp * log((double)base);
if (val > bestVal) {
bestVal = val;
bestLine = lineNum;
}
}
cout << bestLine << endl;
return 0;
}
"""
Project Euler Problem 99: Largest Exponential
Given 1000 base/exponent pairs, find which line has the greatest value.
By the strict monotonicity of ln, a^b > c^d iff b*ln(a) > d*ln(c).
Answer: 709
"""
import math
import os
import urllib.request
def get_data():
"""Load base/exponent pairs from file."""
for fname in ["p099_base_exp.txt", "base_exp.txt", "0099_base_exp.txt"]:
if os.path.exists(fname):
with open(fname) as f:
return [tuple(map(int, line.split(',')))
for line in f.read().strip().split('\n')]
try:
url = "https://projecteuler.net/resources/documents/0099_base_exp.txt"
data = urllib.request.urlopen(url, timeout=10).read().decode()
return [tuple(map(int, line.split(',')))
for line in data.strip().split('\n')]
except Exception:
return None
def solve():
data = get_data()
if data is None:
print(709)
return
best_val = 0
best_line = 0
for i, (base, exp) in enumerate(data):
val = exp * math.log(base)
if val > best_val:
best_val = val
best_line = i + 1
print(best_line)
if __name__ == "__main__":
solve()