Powerful Digit Counts
How many n -digit positive integers exist which are also an n -th power?
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 has exactly digits if and only if and , where
No solution exists for or .
Proof. The number has exactly digits if and only if
Step 1: Bounding . The right inequality of gives , hence . Since must be a positive integer, . For , we have , so has at least digits. For , is not a positive integer.
Step 2: Bounding for fixed . Taking of the left inequality:
Case : , so the constraint becomes . Indeed, is a 1-digit number, but has only 1 digit for all , failing .
Case : Here , so . The right inequality is automatically satisfied since . We must verify that for every integer with , the left inequality holds. Since implies , exponentiating gives . Thus every such is valid, and is exact.
Lemma 1 (Explicit enumeration). The values are:
| 1 | 0.0000 | 1.000 | 1 |
| 2 | 0.3010 | 1.431 | 1 |
| 3 | 0.4771 | 1.912 | 1 |
| 4 | 0.6021 | 2.513 | 2 |
| 5 | 0.6990 | 3.322 | 3 |
| 6 | 0.7782 | 4.507 | 4 |
| 7 | 0.8451 | 6.456 | 6 |
| 8 | 0.9031 | 10.319 | 10 |
| 9 | 0.9542 | 21.854 | 21 |
Proof. Direct computation using . Each entry is verified by checking has digits and does not have digits.
Theorem 2 (Total count). The number of pairs with an -digit positive integer is
Proof. Sum the column from Lemma 1. By Theorem 1, this enumeration is exhaustive: no contributes, and for each , every valid is counted.
Editorial
The theorem reduces the search to bases through , 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 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: . The algorithm performs exactly 9 iterations, each involving one logarithm, one division, and one floor operation.
Space: . Only scalar variables are used.
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() {
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;
}
"""
Problem 63: Powerful Digit Counts
How many n-digit positive integers exist which are also an nth power?
"""
import math
def solve():
count = 0
for a in range(1, 10):
if a == 1:
count += 1
else:
# N(a) = floor(1 / (1 - log10(a)))
count += int(math.floor(1.0 / (1.0 - math.log10(a))))
return count
# Verification by brute force
def solve_brute():
count = 0
for a in range(1, 10):
for n in range(1, 100):
if len(str(a ** n)) == n:
count += 1
return count
answer = solve()
assert answer == solve_brute(), f"Mismatch: {answer} vs {solve_brute()}"
print(answer)