All Euler problems
Project Euler

Hexadecimal Numbers

In the hexadecimal number system, numbers are represented using 16 digits: 0, 1, 2,..., 9, A, B, C, D, E, F. How many hexadecimal numbers containing at most sixteen hexadecimal digits exist with al...

Source sync Apr 19, 2026
Problem #0162
Level Level 06
Solved By 6,085
Languages C++, Python
Answer \texttt{3D58725572C62302
Length 359 words
brute_forcemodular_arithmeticarithmetic

Problem Statement

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

In the hexadecimal number system numbers are represented using $16$ different digits: $$0,1,2,3,4,5,6,7,8,9,\mathrm A,\mathrm B,\mathrm C,\mathrm D,\mathrm E,\mathrm F.$$ The hexadecimal number $\mathrm{AF}$ when written in the decimal number system equals $10 \times 16 + 15 = 175$.

In the $3$-digit hexadecimal numbers $10\mathrm A$, $1\mathrm A0$, $\mathrm A10$, and $\mathrm A01$ the digits $0$, $1$ and $\mathrm A$ are all present.

Like numbers written in base ten we write hexadecimal numbers without leading zeroes.

How many hexadecimal numbers containing at most sixteen hexadecimal digits exist with all of the digits $0$, $1$, and $\mathrm A$ present at least once?

Give your answer as a hexadecimal number.

($A$, $B$, $C$, $D$, $E$ and $F$ in upper case, without any leading or trailing code that marks the number as hexadecimal and without leading zeroes, e.g. $1A3F$ and not: $1a3f$ and not $0x1a3f$ and not $1A3F$ and not $\#1A3F$ and not $0000001A3F$)

Problem 162: Hexadecimal Numbers

Definitions

Definition 1. An nn-digit hexadecimal number is a string d1d2dnd_1 d_2 \cdots d_n over the alphabet {0,1,,9,A,,F}\{0,1,\ldots,9,A,\ldots,F\} with d10d_1 \neq 0 and n1n \geq 1.

Definition 2. For a subset T{0,1,,F}T \subseteq \{0,1,\ldots,F\} and an nn-digit hex number xx, we write Tdigits(x)T \subseteq \mathrm{digits}(x) to mean every element of TT appears among the digits of xx.

Mathematical Development

Theorem 1 (Inclusion-exclusion formula). Let SnS_n denote the set of nn-digit hex numbers (no leading zero). Let N(n)={xSn:{0,1,A}digits(x)}N(n) = |\{x \in S_n : \{0, 1, A\} \subseteq \mathrm{digits}(x)\}|. Then:

N(n)=1516n115n21415n1+214n+1314n113n.N(n) = 15 \cdot 16^{n-1} - 15^n - 2 \cdot 14 \cdot 15^{n-1} + 2 \cdot 14^n + 13 \cdot 14^{n-1} - 13^n.

Proof. We apply the inclusion-exclusion principle. Define:

A0={xSn:0digits(x)},A1={xSn:1digits(x)},AA={xSn:Adigits(x)}.A_0 = \{x \in S_n : 0 \notin \mathrm{digits}(x)\}, \quad A_1 = \{x \in S_n : 1 \notin \mathrm{digits}(x)\}, \quad A_A = \{x \in S_n : A \notin \mathrm{digits}(x)\}.

Then N(n)=SnA0A1AAN(n) = |S_n| - |A_0 \cup A_1 \cup A_A|, and by inclusion-exclusion:

A0A1AA=AiAiAj+A0A1AA.|A_0 \cup A_1 \cup A_A| = \sum |A_i| - \sum |A_i \cap A_j| + |A_0 \cap A_1 \cap A_A|.

We compute each term. An nn-digit hex number has first digit from {1,,F}\{1,\ldots,F\} (15 choices) and subsequent digits from {0,,F}\{0,\ldots,F\} (16 choices each).

Singleton terms:

  • A0|A_0|: No digit 0 allowed. First digit: 15 choices ({1,,F}\{1,\ldots,F\}). Each subsequent digit: 15 choices ({1,,F}\{1,\ldots,F\}). Total: 15n15^n.
  • A1|A_1|: No digit 1 allowed. First digit: 14 choices ({2,,9,A,,F}{0}{0}={2,,F}{1}\{2,\ldots,9,A,\ldots,F\} \cup \{0\} \setminus \{0\} = \{2,\ldots,F\} \setminus \{1\}, which has 14 elements). Subsequent digits: 15 choices ({0,,F}{1}\{0,\ldots,F\} \setminus \{1\}). Total: 1415n114 \cdot 15^{n-1}.
  • AA|A_A|: No digit AA allowed. By symmetry with A1|A_1| (both exclude one non-zero digit): 1415n114 \cdot 15^{n-1}.

Pairwise terms:

  • A0A1|A_0 \cap A_1|: No 0 or 1. First and all digits from {2,,F}\{2,\ldots,F\} (14 elements). Total: 14n14^n.
  • A0AA|A_0 \cap A_A|: No 0 or AA. Digits from {1,,9,B,,F}\{1,\ldots,9,B,\ldots,F\} (14 elements). Total: 14n14^n.
  • A1AA|A_1 \cap A_A|: No 1 or AA. First digit from {2,,9,B,,F}\{2,\ldots,9,B,\ldots,F\} (13 choices). Subsequent from {0,2,,9,B,,F}\{0,2,\ldots,9,B,\ldots,F\} (14 choices). Total: 1314n113 \cdot 14^{n-1}.

Triple term:

  • A0A1AA|A_0 \cap A_1 \cap A_A|: No 0, 1, or AA. All digits from {2,,9,B,,F}\{2,\ldots,9,B,\ldots,F\} (13 elements). Total: 13n13^n.

Substituting:

N(n)=1516n1(15n+21415n1)+(214n+1314n1)13n.N(n) = 15 \cdot 16^{n-1} - \bigl(15^n + 2 \cdot 14 \cdot 15^{n-1}\bigr) + \bigl(2 \cdot 14^n + 13 \cdot 14^{n-1}\bigr) - 13^n. \qquad \square

Theorem 2 (Summation). The total count is:

T=n=116N(n).T = \sum_{n=1}^{16} N(n).

Proof. The problem asks for hex numbers with at most 16 digits containing all three target digits. A 1-digit number cannot contain three distinct digits, so N(1)=0N(1) = 0, but it is harmless to include in the sum. The result follows by linearity of summation. \square

Corollary (Closed-form summation). Each component of TT is a geometric series. For instance:

n=1161516n1=1516161161=16161.\sum_{n=1}^{16} 15 \cdot 16^{n-1} = 15 \cdot \frac{16^{16} - 1}{16 - 1} = 16^{16} - 1.

Similarly for the other five terms. All intermediate values fit in 64-bit unsigned integers.

Editorial

Count hexadecimal numbers with at most 16 digits containing all of 0, 1, and A at least once. Uses the inclusion-exclusion principle.

Pseudocode

    total = 0
    For n from 1 to 16:
        N_n = 15 * 16^(n-1)
              - 15^n
              - 2 * 14 * 15^(n-1)
              + 2 * 14^n
              + 13 * 14^(n-1)
              - 13^n
        total += N_n
    Return TO_HEX_UPPERCASE(total)

Complexity Analysis

Time. The loop runs 16 iterations, each requiring O(1)O(1) big-integer multiplications. Alternatively, six geometric series can be evaluated in O(1)O(1), making the total cost O(1)O(1).

Space. O(1)O(1).

Answer

3D58725572C62302\boxed{\texttt{3D58725572C62302}}

Code

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

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

/*
 * Project Euler Problem 162: Hexadecimal Numbers
 *
 * Count hex numbers with at most 16 digits containing all of {0, 1, A}.
 * Inclusion-exclusion over missing-digit sets.
 */

typedef unsigned __int128 u128;

int main() {
    auto power = [](u128 base, int exp) -> u128 {
        u128 r = 1;
        for (int i = 0; i < exp; i++) r *= base;
        return r;
    };

    u128 total = 0;
    for (int n = 1; n <= 16; n++) {
        u128 S   = 15 * power(16, n - 1);
        u128 A0  = power(15, n);
        u128 A1  = 14 * power(15, n - 1);
        u128 AA  = 14 * power(15, n - 1);
        u128 A01 = power(14, n);
        u128 A0A = power(14, n);
        u128 A1A = 13 * power(14, n - 1);
        u128 A01A = power(13, n);
        u128 bad = A0 + A1 + AA - A01 - A0A - A1A + A01A;
        total += S - bad;
    }

    // Convert to uppercase hexadecimal
    string hex;
    u128 v = total;
    if (v == 0) hex = "0";
    while (v > 0) {
        int d = (int)(v % 16);
        hex += (d < 10) ? ('0' + d) : ('A' + d - 10);
        v /= 16;
    }
    reverse(hex.begin(), hex.end());
    cout << hex << endl;
    return 0;
}