All Euler problems
Project Euler

Distinct Primes Factors

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?

Source sync Apr 19, 2026
Problem #0047
Level Level 02
Solved By 64,749
Languages C++, Python
Answer 134043
Length 413 words
number_theorybrute_forcelinear_algebra

Problem Statement

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

The first two consecutive numbers to have two distinct prime factors are: \begin {align*} 14 &= 2 \times 7\\ 15 &= 3 \times 5. \end {align*}

The first three consecutive numbers to have three distinct prime factors are: \begin {align*} 644 &= 2^2 \times 7 \times 23\\ 645 &= 3 \times 5 \times 43\\ 646 &= 2 \times 17 \times 19. \end {align*}

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?

Problem 47: Distinct Primes Factors

Mathematical Development

Formal Development

Definition 1 (Distinct prime factor count). For a positive integer nn with canonical prime factorization n=p1a1p2a2prarn = p_1^{a_1} p_2^{a_2} \cdots p_r^{a_r}, the number of distinct prime factors is ω(n)=r\omega(n) = r.

Definition 2 (Consecutive run). A consecutive run of length mm with property ω=c\omega = c starting at nn is a maximal sequence n,n+1,,n+m1n, n+1, \ldots, n+m-1 such that ω(n+j)=c\omega(n+j) = c for all 0jm10 \leq j \leq m-1.

We seek min{nZ+:ω(n)=ω(n+1)=ω(n+2)=ω(n+3)=4}\min\{n \in \mathbb{Z}^+ : \omega(n) = \omega(n+1) = \omega(n+2) = \omega(n+3) = 4\}.

Theorem 1 (Sieve computation of ω\omega). For all integers n[2,N]n \in [2, N], the function ω(n)\omega(n) can be computed simultaneously in O(NloglogN)O(N \log \log N) time and O(N)O(N) space.

Proof. Initialize an array ω[2..N]=0\omega[2..N] = 0. For each p=2,3,4,,Np = 2, 3, 4, \ldots, N: if ω[p]=0\omega[p] = 0 then pp is prime (it has not been marked by any smaller prime), so for every multiple m{p,2p,3p,}[p,N]m \in \{p, 2p, 3p, \ldots\} \cap [p, N], increment ω[m]\omega[m] by 11.

Correctness. Each prime pp dividing nn contributes exactly one increment to ω[n]\omega[n], since nn is a multiple of pp. Conversely, each increment to ω[n]\omega[n] corresponds to a distinct prime divisor of nn. Hence after the sieve, ω[n]\omega[n] equals the number of distinct prime factors.

Complexity. Each prime pp contributes N/p\lfloor N/p \rfloor increments. The total is:

pN, p primeNpNpN1p=N(lnlnN+M+o(1))\sum_{p \leq N,\ p\text{ prime}} \left\lfloor \frac{N}{p} \right\rfloor \leq N \sum_{p \leq N} \frac{1}{p} = N(\ln \ln N + M + o(1))

where M0.2615M \approx 0.2615 is the Meissel—Mertens constant. This is O(NloglogN)O(N \log \log N). Space is O(N)O(N) for the array. \square

Lemma 1 (Lower bound). If ω(n)=4\omega(n) = 4, then n2357=210n \geq 2 \cdot 3 \cdot 5 \cdot 7 = 210.

Proof. An integer with 4 distinct prime factors is at least p1p2p3p4p_1 p_2 p_3 p_4 where p1<p2<p3<p4p_1 < p_2 < p_3 < p_4 are primes. The minimum is attained by the four smallest primes: 2357=2102 \cdot 3 \cdot 5 \cdot 7 = 210. \square

Lemma 2 (Sufficient search bound). The first four consecutive integers each with ω=4\omega = 4 occur below 150,000150{,}000.

Proof. By direct computation (see Theorem 2 below), the answer is 134043<150,000134043 < 150{,}000. \square

Theorem 2. The first four consecutive integers each having exactly 4 distinct prime factors are 134043,134044,134045,134046134043, 134044, 134045, 134046.

Proof. We apply the sieve of Theorem 1 with N=150,000N = 150{,}000, then scan for the first index nn where ω[n]=ω[n+1]=ω[n+2]=ω[n+3]=4\omega[n] = \omega[n+1] = \omega[n+2] = \omega[n+3] = 4.

The factorizations are:

  • 134043=3×7×13×491(ω=4)134043 = 3 \times 7 \times 13 \times 491 \quad (\omega = 4)
  • 134044=22×23×31×47(ω=4)134044 = 2^2 \times 23 \times 31 \times 47 \quad (\omega = 4)
  • 134045=5×11×41×59(ω=4)134045 = 5 \times 11 \times 41 \times 59 \quad (\omega = 4)
  • 134046=2×3×43×521(ω=4)134046 = 2 \times 3 \times 43 \times 521 \quad (\omega = 4)

Each factorization is verified by confirming that 491,23,31,47,41,59,43,521491, 23, 31, 47, 41, 59, 43, 521 are all prime (by trial division up to their respective square roots).

Minimality. The linear scan guarantees that no n<134043n < 134043 satisfies the four-consecutive condition. Indeed, the scan reports the first occurrence. \square

Editorial

We evaluate the function ω(n)\omega(n) for every integer up to a fixed bound by a modified sieve: whenever a prime pp is encountered, each multiple of pp receives one increment in its distinct-factor count. After this preprocessing, a single left-to-right scan locates the first run of four consecutive integers whose counts are all equal to 44, and the first term of that run is returned.

Pseudocode

Algorithm: First Run of Four Integers with Four Distinct Prime Factors
Require: A search bound large enough to contain the first solution.
Ensure: The first integer in the earliest block of four consecutive integers each having exactly four distinct prime factors.
1: Initialize omega(n) ← 0 for every integer in the search range.
2: For each prime p, increment omega(m) for every multiple m of p.
3: Scan the integers in increasing order while maintaining the current streak length of values with omega(n) = 4.
4: When the streak length first reaches 4, return the initial value of that block.

Complexity Analysis

Proposition (Time complexity). The algorithm runs in O(NloglogN)O(N \log \log N) time where N=150,000N = 150{,}000.

Proof. Phase 1 (the sieve) dominates at O(NloglogN)O(N \log \log N) by Theorem 1. Phase 2 is a single linear scan in O(N)O(N) time. The total is O(NloglogN)O(N \log \log N). \square

Proposition (Space complexity). The algorithm uses O(N)O(N) space.

Proof. The ω\omega array of size N+1N + 1 is the sole data structure. \square

Answer

134043\boxed{134043}

Code

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

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

int main() {
    const int LIMIT = 150000;
    vector<int> dpf(LIMIT, 0);

    for (int i = 2; i < LIMIT; i++) {
        if (dpf[i] == 0) {
            for (int j = i; j < LIMIT; j += i) {
                dpf[j]++;
            }
        }
    }

    int consecutive = 0;
    for (int i = 2; i < LIMIT; i++) {
        if (dpf[i] == 4) {
            consecutive++;
            if (consecutive == 4) {
                cout << i - 3 << endl;
                return 0;
            }
        } else {
            consecutive = 0;
        }
    }
    return 0;
}