All Euler problems
Project Euler

Powerful Digit Counts

How many n -digit positive integers exist which are also an n -th power?

Source sync Apr 19, 2026
Problem #0063
Level Level 02
Solved By 48,089
Languages C++, Python
Answer 49
Length 374 words
brute_forcearithmetic

Problem Statement

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

The \(5\)-digit number, \(16807=7^5\), is also a fifth power. Similarly, the \(9\)-digit number, \(134217728=8^9\), is a ninth power.

How many \(n\)-digit positive integers exist which are also an \(n\)th power?

Problem 63: Powerful Digit Counts

Mathematical Foundation

Theorem 1 (Complete characterization). A positive integer ana^n has exactly nn digits if and only if a{1,2,,9}a \in \{1, 2, \ldots, 9\} and 1nN(a)1 \le n \le N(a), where

N(a)={1if a=1,11log10aif 2a9.N(a) = \begin{cases} 1 & \text{if } a = 1, \\ \left\lfloor \dfrac{1}{1 - \log_{10} a} \right\rfloor & \text{if } 2 \le a \le 9. \end{cases}

No solution exists for a10a \ge 10 or a=0a = 0.

Proof. The number ana^n has exactly nn digits if and only if

10n1an<10n.(\star)10^{n-1} \le a^n < 10^n. \tag{\star}

Step 1: Bounding aa. The right inequality of ()(\star) gives an<10na^n < 10^n, hence a<10a < 10. Since aa must be a positive integer, a{1,2,,9}a \in \{1, 2, \ldots, 9\}. For a10a \ge 10, we have an10na^n \ge 10^n, so ana^n has at least n+1n+1 digits. For a=0a = 0, an=0a^n = 0 is not a positive integer.

Step 2: Bounding nn for fixed aa. Taking log10\log_{10} of the left inequality:

n1nlog10a,i.e.,n(1log10a)1.n - 1 \le n \log_{10} a, \quad \text{i.e.,} \quad n(1 - \log_{10} a) \le 1.

Case a=1a = 1: log101=0\log_{10} 1 = 0, so the constraint becomes n1n \le 1. Indeed, 11=11^1 = 1 is a 1-digit number, but 1n=11^n = 1 has only 1 digit for all n2n \ge 2, failing ()(\star).

Case 2a92 \le a \le 9: Here 0<1log10a<10 < 1 - \log_{10} a < 1, so n11log10an \le \frac{1}{1 - \log_{10} a}. The right inequality an<10na^n < 10^n is automatically satisfied since a<10a < 10. We must verify that for every integer nn with 1nN(a)1 \le n \le N(a), the left inequality 10n1an10^{n-1} \le a^n holds. Since n(1log10a)1n(1 - \log_{10} a) \le 1 implies n1nlog10an - 1 \le n \log_{10} a, exponentiating gives 10n1an10^{n-1} \le a^n. Thus every such nn is valid, and N(a)=1/(1log10a)N(a) = \lfloor 1/(1 - \log_{10} a) \rfloor is exact. \square

Lemma 1 (Explicit enumeration). The values N(a)N(a) are:

aalog10a\log_{10} a11log10a\frac{1}{1 - \log_{10} a}N(a)N(a)
10.00001.0001
20.30101.4311
30.47711.9121
40.60212.5132
50.69903.3223
60.77824.5074
70.84516.4566
80.903110.31910
90.954221.85421

Proof. Direct computation using log10a=lna/ln10\log_{10} a = \ln a / \ln 10. Each entry is verified by checking aN(a)a^{N(a)} has N(a)N(a) digits and aN(a)+1a^{N(a)+1} does not have N(a)+1N(a)+1 digits. \square

Theorem 2 (Total count). The number of pairs (a,n)(a, n) with ana^n an nn-digit positive integer is

a=19N(a)=1+1+1+2+3+4+6+10+21=49.\sum_{a=1}^{9} N(a) = 1 + 1 + 1 + 2 + 3 + 4 + 6 + 10 + 21 = 49.

Proof. Sum the column N(a)N(a) from Lemma 1. By Theorem 1, this enumeration is exhaustive: no a10a \ge 10 contributes, and for each a9a \le 9, every valid nn is counted. \square

Editorial

The theorem reduces the search to bases 11 through 99, and for each such base the admissible exponents are counted directly by a logarithmic formula. The algorithm therefore does not enumerate powers. Instead, it computes the contribution of each base and adds those contributions together, with the base 11 handled separately because it contributes exactly one valid exponent.

Pseudocode

Initialize the total count to zero.

For each base a from 1 through 9:
    if a is 1:
        contribute exactly one valid exponent
    otherwise:
        compute the largest admissible exponent from the logarithmic bound

    add that contribution to the running total

Return the final total.

Complexity Analysis

Time: O(1)O(1). The algorithm performs exactly 9 iterations, each involving one logarithm, one division, and one floor operation.

Space: O(1)O(1). Only scalar variables are used.

Answer

49\boxed{49}

Code

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

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

int main() {
    int count = 0;
    for (int a = 1; a <= 9; a++) {
        if (a == 1) {
            count += 1;
            continue;
        }
        // N(a) = floor(1 / (1 - log10(a)))
        double log10a = log10((double)a);
        int n_max = (int)floor(1.0 / (1.0 - log10a));
        count += n_max;
    }
    cout << count << endl;
    return 0;
}