All Euler problems
Project Euler

Distinct Powers

Consider all integer combinations of a^b for 2 <= a <= 100 and 2 <= b <= 100. How many distinct terms are in the sequence?

Source sync Apr 19, 2026
Problem #0029
Level Level 01
Solved By 115,916
Languages C++, Python
Answer 9183
Length 589 words
brute_forcelinear_algebranumber_theory

Problem Statement

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

Consider all integer combinations of $a^b$ for $2 \le a \le 5$ and $2 \le b \le 5$: $$ \begin{matrix} 2^2=4, & 2^3=8, & 2^4=16, & 2^5=32\\ 3^2=9, & 3^3=27, & 3^4=81, & 3^5=243\\ 4^2=16, & 4^3=64, & 4^4=256, & 4^5=1024\\ 5^2=25, & 5^3=125, & 5^4=625, & 5^5=3125 \end{matrix} $$

If they are then placed in numerical order, with any repeats removed, we get the following sequence of $15$ distinct terms: $$4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125.$$

How many distinct terms are in the sequence generated by $a^b$ for $2 \le a \le 100$ and $2 \le b \le 100$?

Problem 29: Distinct Powers

Mathematical Development

Definition 1 (Primitive Base). An integer c2c \geq 2 is a primitive base if there do not exist integers d2d \geq 2 and k2k \geq 2 such that c=dkc = d^k. Equivalently, cc is primitive if it is not a perfect power.

Definition 2 (Canonical Representation). For any integer a2a \geq 2, the canonical representation is the unique pair (c,p)(c, p) where cc is a primitive base and p1p \geq 1 is an integer such that a=cpa = c^p. Uniqueness follows from the fundamental theorem of arithmetic.

Proof of Uniqueness. Suppose a=c1p1=c2p2a = c_1^{p_1} = c_2^{p_2} with c1,c2c_1, c_2 primitive. Write c1=qiαic_1 = \prod q_i^{\alpha_i} and c2=qiβic_2 = \prod q_i^{\beta_i} in terms of prime factorizations. Then p1αi=p2βip_1 \alpha_i = p_2 \beta_i for all ii. If c1c2c_1 \neq c_2, then αi/βi\alpha_i / \beta_i is constant and equals p2/p1p_2 / p_1 for all ii, implying c1=c2p2/p1c_1 = c_2^{p_2/p_1}. Since c1c_1 is primitive, p2/p1=1p_2/p_1 = 1 and c1=c2c_1 = c_2, a contradiction. \square

Theorem 1 (Collision Criterion). Two pairs (a1,b1)(a_1, b_1) and (a2,b2)(a_2, b_2) satisfy a1b1=a2b2a_1^{b_1} = a_2^{b_2} if and only if a1a_1 and a2a_2 share the same primitive base cc, say a1=cp1a_1 = c^{p_1} and a2=cp2a_2 = c^{p_2}, and p1b1=p2b2p_1 b_1 = p_2 b_2.

Proof. (\Leftarrow) If a1=cp1a_1 = c^{p_1} and a2=cp2a_2 = c^{p_2} with p1b1=p2b2p_1 b_1 = p_2 b_2, then a1b1=cp1b1=cp2b2=a2b2a_1^{b_1} = c^{p_1 b_1} = c^{p_2 b_2} = a_2^{b_2}.

(\Rightarrow) Suppose a1b1=a2b2a_1^{b_1} = a_2^{b_2}. Let a1=c1p1a_1 = c_1^{p_1} and a2=c2p2a_2 = c_2^{p_2} be canonical. Then c1p1b1=c2p2b2c_1^{p_1 b_1} = c_2^{p_2 b_2}. By uniqueness of the canonical representation for the value a1b1a_1^{b_1}, we must have c1=c2c_1 = c_2 and p1b1=p2b2p_1 b_1 = p_2 b_2. \square

Definition 3 (Base Group). For a primitive base cc, define the base group GcG_c as the set of all values {a[2,100]:a=cp for some p1}\{a \in [2, 100] : a = c^p \text{ for some } p \geq 1\}, and let Kc=max{p:cp100}K_c = \max\{p : c^p \leq 100\}.

Theorem 2 (Reduction to Exponent Counting). The number of distinct terms in {ab:2a100,2b100}\{a^b : 2 \leq a \leq 100, 2 \leq b \leq 100\} equals

c primitiveEc,\sum_{c \text{ primitive}} |E_c|,

where Ec={pb:1pKc,  2b100}E_c = \{p \cdot b : 1 \leq p \leq K_c, \; 2 \leq b \leq 100\} is the set of distinct effective exponents.

Proof. By Theorem 1, aba^b is uniquely determined by its primitive base cc and effective exponent pbpb. Two terms from different base groups are always distinct (different primitive bases). Within a base group, two terms collide iff they share the same effective exponent. Hence the count of distinct terms from base cc is Ec|E_c|, and the total is the sum over all primitive bases. \square

Lemma 1 (Primitive Bases with Kc2K_c \geq 2). The primitive bases cc with at least two powers in [2,100][2, 100] (i.e., Kc2K_c \geq 2) are:

ccPowers in [2,100][2,100]KcK_c
22, 4, 8, 16, 32, 646
33, 9, 27, 814
55, 252
66, 362
77, 492
1010, 1002

All other primitive bases c[2,100]c \in [2, 100] satisfy Kc=1K_c = 1, so their base group contributes exactly 9999 distinct terms (the values c2,c3,,c100c^2, c^3, \ldots, c^{100}, all distinct).

Lemma 2 (Exponent Set for Kc=2K_c = 2). For Kc=2K_c = 2, Ec={1b:2b100}{2b:2b100}=[2,100][4,200]E_c = \{1 \cdot b : 2 \leq b \leq 100\} \cup \{2 \cdot b : 2 \leq b \leq 100\} = [2, 100] \cup [4, 200]. Thus Ec=99+99{4,6,8,,100}=19849=149|E_c| = 99 + 99 - |\{4, 6, 8, \ldots, 100\}| = 198 - 49 = 149.

Proof. The set [2,100][2, 100] has 99 elements and [4,200][4, 200] has 99 elements. Their intersection is {2k:2k50}[2,100]={4,6,,100}\{2k : 2 \leq k \leq 50\} \cap [2, 100] = \{4, 6, \ldots, 100\}, which has 49 elements. By inclusion-exclusion, Ec=99+9949=149|E_c| = 99 + 99 - 49 = 149. \square

Theorem 3 (Answer). The number of distinct terms is 91839183.

Proof. Computed by summing Ec|E_c| over all primitive bases cc with 2c11002 \leq c^1 \leq 100. For bases with Kc=1K_c = 1, each contributes 99. For the six bases with Kc2K_c \geq 2, we compute Ec|E_c| by explicit enumeration of the exponent sets. The total is verified by direct computation (e.g., using arbitrary-precision arithmetic to store all aba^b in a set). \square

Editorial

We keep two equivalent viewpoints. The direct method enumerates every pair (a,b)(a, b) with 2a,b1002 \le a, b \le 100 and inserts aba^b into a set, while the canonical method rewrites each base as a=cpa = c^p with primitive base cc and records the exponent products pbpb grouped by cc. This is sufficient because two powers are equal exactly when they share the same canonical base and exponent.

Pseudocode

Algorithm: Distinct Powers by Direct Enumeration
Require: Positive integers A ≥ 2 and B ≥ 2.
Ensure: The cardinality of {a^b : 2 ≤ a ≤ A, 2 ≤ b ≤ B}.
1: Initialize an empty set S.
2: For each base a in {2, 3, ..., A} do:
3:     For each exponent b in {2, 3, ..., B}, insert a^b into S.
4: Return |S|.
Algorithm: Distinct Powers by Canonical Exponents
Require: Positive integers A ≥ 2 and B ≥ 2.
Ensure: The cardinality of {a^b : 2 ≤ a ≤ A, 2 ≤ b ≤ B}.
1: Initialize a map from primitive bases to sets of canonical exponents.
2: For each base a in {2, 3, ..., A}, write a uniquely as c^p with primitive base c.
3:     For each exponent b in {2, 3, ..., B}, insert p · b into the exponent set attached to c.
4: Return the total size of all exponent sets.

Complexity Analysis

Proposition (Method 1). Using arbitrary-precision integers, the algorithm runs in O(N2M)O(N^2 M) time and O(N2M)O(N^2 M) space, where N=A1=99N = A - 1 = 99 and M=O(BlogA)M = O(B \log A) is the bit-length of the largest value ABA^B.

Proof. There are N2N^2 pairs. Each exponentiation aba^b produces a number with O(BlogA)O(B \log A) bits, and hashing/comparing such a number takes O(M)O(M) time. The set stores at most N2N^2 entries, each of size O(M)O(M). \square

Proposition (Method 2). Using canonical representations, the algorithm runs in O(N2)O(N^2) time and O(N2)O(N^2) space, with all arithmetic on O(logN)O(\log N)-bit integers.

Proof. Computing the canonical representation of each aa takes O(aloga)O(\sqrt{a} \log a) time. The double loop inserts N2N^2 entries (each an integer KcB=O(BlogA)\leq K_c \cdot B = O(B \log A)) into sets keyed by primitive base. Total insertions: O(N2)O(N^2). \square

Answer

9183\boxed{9183}

Code

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

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

int main() {
    // Canonical representation: for each a, find primitive base c and power p
    // such that a = c^p. Then a^b = c^(p*b).
    vector<pair<int,int>> canon(101); // canon[a] = (base, power)

    for (int a = 2; a <= 100; a++) {
        canon[a] = {a, 1};
        for (int c = 2; c * c <= a; c++) {
            int val = c, k = 1;
            while (val < a) { val *= c; k++; }
            if (val == a) {
                auto [base, base_k] = canon[c];
                canon[a] = {base, base_k * k};
                break;
            }
        }
    }

    map<int, set<int>> exponents;
    for (int a = 2; a <= 100; a++) {
        auto [base, k] = canon[a];
        for (int b = 2; b <= 100; b++)
            exponents[base].insert(k * b);
    }

    int total = 0;
    for (auto& [base, s] : exponents)
        total += s.size();

    cout << total << endl;
    return 0;
}