All Euler problems
Project Euler

Generalised Hamming Numbers

A Hamming number is a positive integer which has no prime factor larger than 5. We define a type- t generalised Hamming number as a positive integer which has no prime factor larger than t. How man...

Source sync Apr 19, 2026
Problem #0204
Level Level 05
Solved By 8,215
Languages C++, Python
Answer 2944730
Length 307 words
number_theorysearchrecursion

Problem Statement

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

A Hamming number is a positive number which has no prime factor larger than \(5\).

So the first few Hamming numbers are \(1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15\).

There are \(1105\) Hamming numbers not exceeding \(10^8\).

We will call a positive number a generalised Hamming number of type \(n\), if it has no prime factor larger than \(n\).

Hence the Hamming numbers are the generalised Hamming numbers of type \(5\).

How many generalised Hamming numbers of type \(100\) are there which don’t exceed \(10^9\)?

Problem 204: Generalised Hamming Numbers

Mathematical Foundation

Theorem (Fundamental Theorem of Arithmetic, applied to smooth numbers). Every 100-smooth number n1n \geq 1 can be uniquely written as n=p1a1p2a2p25a25n = p_1^{a_1} p_2^{a_2} \cdots p_{25}^{a_{25}} where ai0a_i \geq 0 and p1<p2<<p25p_1 < p_2 < \cdots < p_{25} are the 25 primes not exceeding 100. The set of 100-smooth numbers up to NN is therefore in bijection with the set of 25-tuples (a1,,a25)Z025(a_1, \ldots, a_{25}) \in \mathbb{Z}_{\geq 0}^{25} satisfying i=125piaiN\prod_{i=1}^{25} p_i^{a_i} \leq N.

Proof. This follows directly from the Fundamental Theorem of Arithmetic. A positive integer is 100-smooth iff all its prime factors lie in {p1,,p25}={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}\{p_1, \ldots, p_{25}\} = \{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97\}. The uniqueness of prime factorization establishes the bijection. \square

Theorem (Recursive Counting). Define Ψ(N,i)\Psi(N, i) as the number of positive integers N\leq N whose prime factors all lie in {pi,pi+1,,p25}\{p_i, p_{i+1}, \ldots, p_{25}\}. Then:

Ψ(N,i)=a=0logpiNΨ ⁣(N/pia,i+1)\Psi(N, i) = \sum_{a=0}^{\lfloor \log_{p_i} N \rfloor} \Psi\!\left(\left\lfloor N / p_i^a \right\rfloor, \, i+1\right)

with base case Ψ(N,26)=1\Psi(N, 26) = 1 for all N1N \geq 1, and Ψ(N,i)=0\Psi(N, i) = 0 for N<1N < 1. The total count of 100-smooth numbers N\leq N is Ψ(N,1)\Psi(N, 1).

Proof. We partition the smooth numbers by the exponent aa of prime pip_i. For a fixed aa, the number n=piamn = p_i^a \cdot m where mm is a positive integer N/pia\leq N/p_i^a with all prime factors in {pi+1,,p25}\{p_{i+1}, \ldots, p_{25}\}. The count of such mm is Ψ(N/pia,i+1)\Psi(\lfloor N/p_i^a \rfloor, i+1). Summing over valid aa (from 0 to logpiN\lfloor \log_{p_i} N \rfloor) gives the recurrence. The base case holds because with no primes left, the only valid number is m=1m = 1. \square

Lemma (Exponent Bounds). For each prime pip_i, the maximum exponent satisfying pia109p_i^a \leq 10^9 is amax(pi)=logpi109a_{\max}(p_i) = \lfloor \log_{p_i} 10^9 \rfloor. In particular: amax(2)=29a_{\max}(2) = 29, amax(3)=18a_{\max}(3) = 18, amax(5)=12a_{\max}(5) = 12, amax(7)=10a_{\max}(7) = 10, amax(97)=4a_{\max}(97) = 4.

Proof. Direct computation: 229=536870912<109<2302^{29} = 536870912 < 10^9 < 2^{30}, 318=387420489<109<3193^{18} = 387420489 < 10^9 < 3^{19}, etc. For p=97p = 97: 974=88529281<109<975=858734025797^4 = 88529281 < 10^9 < 97^5 = 8587340257. \square

Editorial

Count 100-smooth numbers up to 10^9. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

Input: N = 10^9, primes = [2, 3, 5, 7, 11, ..., 97] (25 primes)
Output: count of 100-smooth numbers <= N

Complexity Analysis

  • Time: Each recursive call corresponds to a distinct 100-smooth number (the leaves of the recursion tree), plus internal nodes. The total number of calls is O(Ψ(N,100))O(\Psi(N, 100)), the answer itself, which is approximately 2.94×1062.94 \times 10^6.
  • Space: O(25)O(25) for the recursion stack (depth equals the number of primes).

Answer

2944730\boxed{2944730}

Code

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

C++ project_euler/problem_204/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
long long LIMIT = 1000000000LL;
int dfs(int idx, long long cur) {
    if (idx == 25) return 1;
    int count = 0;
    long long power = 1;
    while (cur * power <= LIMIT) {
        count += dfs(idx + 1, cur * power);
        power *= primes[idx];
    }
    return count;
}
int main() {
    int answer = dfs(0, 1);
    assert(answer == 2944730);
    cout << answer << endl;
    return 0;
}