All Euler problems
Project Euler

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).

Source sync Apr 19, 2026
Problem #0099
Level Level 02
Solved By 34,205
Languages C++, Python
Answer 709
Length 268 words
analytic_mathbrute_forcegeometry

Problem Statement

This archive keeps the full statement, math, and original media on the page.

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

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

Problem 99: Largest Exponential

Mathematical Foundation

Theorem 1 (Logarithmic comparison of exponentials). For positive reals a,c>0a, c > 0 and positive integers b,d>0b, d > 0:

ab>cd    blna>dlnc.a^b > c^d \iff b \ln a > d \ln c.

Proof. The natural logarithm ln:(0,)R\ln : (0, \infty) \to \mathbb{R} is strictly monotonically increasing (its derivative is 1/x>01/x > 0 for all x>0x > 0). Therefore, for any u,v>0u, v > 0:

u>v    lnu>lnv.u > v \iff \ln u > \ln v.

Since ab>0a^b > 0 and cd>0c^d > 0, we apply this equivalence:

ab>cd    ln(ab)>ln(cd)    blna>dlnc.a^b > c^d \iff \ln(a^b) > \ln(c^d) \iff b \ln a > d \ln c.

The last step uses the logarithm identity ln(xn)=nlnx\ln(x^n) = n \ln x, valid for x>0x > 0 and any real nn. \square

Corollary 1 (Reduction to linear comparison). The line ii^* with the greatest value satisfies

i=argmaxi{1,,1000}bilnai.i^* = \arg\max_{i \in \{1,\ldots,1000\}} b_i \ln a_i.

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 vi=bilnaiv_i = b_i \ln a_i lie in the range [0,106ln(106)][0,1.4×107][0, 10^6 \cdot \ln(10^6)] \approx [0, 1.4 \times 10^7]. Double-precision relative error is bounded by εmach=2522.2×1016\varepsilon_{\text{mach}} = 2^{-52} \approx 2.2 \times 10^{-16}. The absolute error in each viv_i is at most vi3εmach<1071015=108|v_i| \cdot 3\varepsilon_{\text{mach}} < 10^7 \cdot 10^{-15} = 10^{-8} (accounting for errors in ln\ln and the multiplication). For the comparison vi>vjv_i > v_j to fail, we would need vivj<2×108|v_i - v_j| < 2 \times 10^{-8}, which does not occur for the given dataset (the top values are well-separated). \square

Editorial

The numbers themselves are far too large to compare directly, but logarithms turn the comparison into a linear one. Instead of comparing aba^b and cdc^d, we compare blnab\ln a and dlncd\ln c, which preserves the ordering exactly.

That reduces the whole task to a single pass over the file. Each line produces one score blnab\ln a, 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: O(n)O(n) where n=1000n = 1000. Each line requires one logarithm evaluation and one multiplication, both O(1)O(1).

Space: O(1)O(1) — only the current maximum value and line number are stored.

Answer

709\boxed{709}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_099/solution.cpp
#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;
}