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...
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 -digit hexadecimal number is a string over the alphabet with and .
Definition 2. For a subset and an -digit hex number , we write to mean every element of appears among the digits of .
Mathematical Development
Theorem 1 (Inclusion-exclusion formula). Let denote the set of -digit hex numbers (no leading zero). Let . Then:
Proof. We apply the inclusion-exclusion principle. Define:
Then , and by inclusion-exclusion:
We compute each term. An -digit hex number has first digit from (15 choices) and subsequent digits from (16 choices each).
Singleton terms:
- : No digit 0 allowed. First digit: 15 choices (). Each subsequent digit: 15 choices (). Total: .
- : No digit 1 allowed. First digit: 14 choices (, which has 14 elements). Subsequent digits: 15 choices (). Total: .
- : No digit allowed. By symmetry with (both exclude one non-zero digit): .
Pairwise terms:
- : No 0 or 1. First and all digits from (14 elements). Total: .
- : No 0 or . Digits from (14 elements). Total: .
- : No 1 or . First digit from (13 choices). Subsequent from (14 choices). Total: .
Triple term:
- : No 0, 1, or . All digits from (13 elements). Total: .
Substituting:
Theorem 2 (Summation). The total count is:
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 , but it is harmless to include in the sum. The result follows by linearity of summation.
Corollary (Closed-form summation). Each component of is a geometric series. For instance:
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 big-integer multiplications. Alternatively, six geometric series can be evaluated in , making the total cost .
Space. .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 162: Hexadecimal Numbers
Count hexadecimal numbers with at most 16 digits containing all of 0, 1, and A
at least once. Uses the inclusion-exclusion principle.
"""
def solve():
total = 0
for n in range(1, 17):
S = 15 * 16 ** (n - 1)
A0 = 15 ** n
A1 = 14 * 15 ** (n - 1)
AA = 14 * 15 ** (n - 1)
A01 = 14 ** n
A0A = 14 ** n
A1A = 13 * 14 ** (n - 1)
A01A = 13 ** n
bad = A0 + A1 + AA - A01 - A0A - A1A + A01A
total += S - bad
print(f"{total:X}")
solve()