All Euler problems
Project Euler

Powerful Digit Sum

A digital sum (or digit sum) of a natural number is the sum of its decimal digits. Considering natural numbers of the form a^b, where a, b < 100, find the maximum digital sum.

Source sync May 21, 2026
Problem #0056
Level Level 02
Solved By 64,487
Languages C++, Python
Answer 972
Length 460 words
modular_arithmeticlinear_algebrasearch

Problem Statement

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

A googol (\(10^{100}\)) is a massive number: one followed by one-hundred zeros; \(100^{100}\) is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only \(1\).

Considering natural numbers of the form, \(a^b\), where \(a, b < 100\), what is the maximum digital sum?

Problem 56: Powerful Digit Sum

Mathematical Development

Definition 1 (Digital Sum). Let nn be a positive integer with decimal representation n=∑i=0kdi⋅10in = \sum_{i=0}^{k} d_i \cdot 10^i, where 0≤di≤90 \leq d_i \leq 9 and dk≠0d_k \neq 0. The digital sum of nn is

S(n)=∑i=0kdi.S(n) = \sum_{i=0}^{k} d_i.

Theorem 1 (Digital Sum Congruence). For every positive integer nn, S(n)≡n(mod9)S(n) \equiv n \pmod{9}.

Proof. Since 10≡1(mod9)10 \equiv 1 \pmod{9}, it follows that 10i≡1i=1(mod9)10^i \equiv 1^i = 1 \pmod{9} for all i≥0i \geq 0. Therefore

n=∑i=0kdi⋅10i≡∑i=0kdi⋅1=S(n)(mod9).□n = \sum_{i=0}^{k} d_i \cdot 10^i \equiv \sum_{i=0}^{k} d_i \cdot 1 = S(n) \pmod{9}. \qquad \square

Lemma 1 (Digit Count). For a≥2a \geq 2 and b≥1b \geq 1, the number of decimal digits of aba^b is

D(a,b)=⌊blog⁡10a⌋+1.D(a,b) = \lfloor b \log_{10} a \rfloor + 1.

Proof. A positive integer mm has exactly ⌊log⁡10m⌋+1\lfloor \log_{10} m \rfloor + 1 decimal digits, since 10d≤m<10d+110^d \leq m < 10^{d+1} iff d=⌊log⁡10m⌋d = \lfloor \log_{10} m \rfloor. Applying this to m=abm = a^b and using log⁡10(ab)=blog⁡10a\log_{10}(a^b) = b \log_{10} a yields the result. □\square

Theorem 2 (Upper Bound on Digital Sum). For any positive integer nn with DD decimal digits, S(n)≤9DS(n) \leq 9D. Consequently, for a,b<100a, b < 100,

S(ab)≤9⋅D(a,b)≤9(⌊99log⁡1099⌋+1)=9×198=1782.S(a^b) \leq 9 \cdot D(a,b) \leq 9(\lfloor 99 \log_{10} 99 \rfloor + 1) = 9 \times 198 = 1782.

Proof. Each digit satisfies di≤9d_i \leq 9, so S(n)=∑i=0D−1di≤9DS(n) = \sum_{i=0}^{D-1} d_i \leq 9D. The maximum digit count over a,b<100a, b < 100 is achieved at a=b=99a = b = 99: D(99,99)=⌊99×1.99564…⌋+1=197+1=198D(99,99) = \lfloor 99 \times 1.99564\ldots \rfloor + 1 = 197 + 1 = 198. □\square

Lemma 2 (Elimination of Degenerate Cases).

(i) S(1b)=1S(1^b) = 1 for all b≥1b \geq 1.

(ii) If a=10ma = 10^m for some m≥1m \geq 1, then S(ab)=1S(a^b) = 1 for all b≥1b \geq 1.

(iii) S(a1)≤9⋅⌈log⁡10a⌉≤18S(a^1) \leq 9 \cdot \lceil \log_{10} a \rceil \leq 18 for all a<100a < 100.

Proof. (i) 1b=11^b = 1, whose only digit is 1. (ii) (10m)b=10mb(10^m)^b = 10^{mb} is represented as a 1 followed by mbmb zeros, so S(10mb)=1S(10^{mb}) = 1. (iii) For a<100a < 100, aa has at most 2 digits, so S(a)≤18S(a) \leq 18. All three bounds are far below the achievable maximum. □\square

Proposition 1 (Heuristic on Digit Distribution). For “generic” bases aa (i.e., log⁡10a\log_{10} a irrational), the digits of aba^b are heuristically approximately uniformly distributed over {0,1,…,9}\{0, 1, \ldots, 9\}, giving an expected digit sum of approximately 4.5⋅D(a,b)4.5 \cdot D(a,b). Maximizing S(ab)S(a^b) thus requires both a large digit count (large aa and bb) and a favorable deviation toward higher digits.

Remark. This heuristic is supported by the equidistribution of the sequence {blog⁡10a}\{b \log_{10} a\} modulo 1 (Weyl’s theorem) when log⁡10a\log_{10} a is irrational, which governs the leading digit via Benford’s law. Full uniformity of all digits is not rigorously established but is empirically observed.

Theorem 3 (Exhaustive Search Sufficiency). The search space {(a,b):2≤a≤99,  1≤b≤99}\{(a,b) : 2 \leq a \leq 99,\; 1 \leq b \leq 99\} has 98×99=970298 \times 99 = 9702 pairs. For each pair, computing S(ab)S(a^b) via big-integer arithmetic and digit extraction is feasible, and the maximum over all pairs yields the answer.

Proof. By Lemma 2, restricting to a≥2a \geq 2 excludes only the trivially small S(1b)=1S(1^b) = 1. The pair count is bounded, and each evaluation involves a big integer of at most 198 digits. □\square

Editorial

We exhaustively examine all pairs (a,b)(a,b) with 2≤a<1002 \leq a < 100 and 1≤b<1001 \leq b < 100. For each fixed base aa, the powers are built incrementally by repeated multiplication so that aba^b is obtained from ab−1a^{b-1} without recomputing from scratch, and the decimal digit sum of each power is evaluated. The maximum digit sum observed over the whole search is the answer.

Pseudocode

Algorithm: Maximum Digital Sum of a^b with a, b < 100
Require: The integer ranges 2 ≤ a < 100 and 1 ≤ b < 100.
Ensure: The maximum decimal digit sum among all powers a^b in the specified range.
1: Initialize best ← 0.
2: For each base a in {2, 3, ..., 99}, initialize value ← 1.
3: For each exponent b in {1, 2, ..., 99}, update value ← a · value and compute s ← the sum of the decimal digits of value.
4: If s > best, update best ← s.
5: Return best.

Complexity Analysis

Theorem 4 (Time Complexity). The algorithm runs in O(N2⋅Dmax⁡)O(N^2 \cdot D_{\max}) arithmetic operations on individual digits, where N=98N = 98 and Dmax⁡=198D_{\max} = 198.

Proof. The outer loops iterate over 98×98=960498 \times 98 = 9604 pairs (we start bb at 2, but include b=1b = 1 implicitly through the initialization). At each iteration, we perform one multiplication of a DD-digit number by a constant a<100a < 100, which requires O(D)O(D) single-digit multiplications and carry propagations. The digit sum extraction also requires O(D)O(D) operations. Since D≤Dmax⁡=198D \leq D_{\max} = 198, the total work is O(98×98×198)≈1.9×106O(98 \times 98 \times 198) \approx 1.9 \times 10^6. □\square

Space: O(Dmax⁡)O(D_{\max}) for storing the current big integer (at most 198 digits).

Answer

972\boxed{972}

Code

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

C++ project_euler/problem_056/solution.cpp
#include <bits/stdc++.h>
using namespace std;

// Multiply a big integer (stored as digits, least significant first) by a small integer x.
vector<int> multiply(const vector<int>& num, int x) {
    vector<int> result;
    int carry = 0;
    for (int d : num) {
        int prod = d * x + carry;
        result.push_back(prod % 10);
        carry = prod / 10;
    }
    while (carry > 0) {
        result.push_back(carry % 10);
        carry /= 10;
    }
    return result;
}

int digitSum(const vector<int>& num) {
    int s = 0;
    for (int d : num) s += d;
    return s;
}

int main() {
    int maxSum = 0;

    for (int a = 2; a < 100; a++) {
        vector<int> power = {1};  // a^0 = 1
        for (int b = 1; b < 100; b++) {
            power = multiply(power, a);
            int s = digitSum(power);
            if (s > maxSum) {
                maxSum = s;
            }
        }
    }

    cout << maxSum << endl;
    return 0;
}