Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #47 hard

Distinct Primes Factors

The first two consecutive numbers to have two distinct prime factors are:

  • 14 = 2 × 7
  • 15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

  • 644 = 2² × 7 × 23
  • 645 = 3 × 5 × 43
  • 646 = 2 × 17 × 19

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

View on Project Euler

Implementations

cpp
// https://projecteuler.net/problem=47
// The first two consecutive numbers to have two distinct prime factors are:
// 14 = 2 × 7
// 15 = 3 × 5.
// The first three consecutive numbers to have three distinct prime factors are:
// 644 = 2² × 7 × 23
// 645 = 3 × 5 × 43
// 646 = 2 × 17 × 19.
// Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
// Answer: 134043
// Authored by: Tim Varley 💘
#include <iostream>
#include <vector>
#include <set>
int count_distinct_factors(int n) {
std::set<int> factors;
int num = n;
for (int i = 2; i * i <= num; ++i) {
while (num % i == 0) {
factors.insert(i);
num /= i;
}
}
if (num > 1) factors.insert(num);
return factors.size();
}
int distinct_prime_factors() {
int n = 2;
while (true) {
if (count_distinct_factors(n) == 4 &&
count_distinct_factors(n+1) == 4 &&
count_distinct_factors(n+2) == 4 &&
count_distinct_factors(n+3) == 4) {
return n;
}
++n;
}
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << distinct_prime_factors() << std::endl;
}
#endif // UNITTEST_MODE
View on GitHub
O(n) time complexity

Solution Notes

Mathematical Background

The problem asks for the first of four consecutive integers where each has exactly four distinct prime factors.

Examples given:

  • Two consecutive with two factors: 14 = 2×7, 15 = 3×5
  • Three consecutive with three factors: 644 = 2²×7×23, 645 = 3×5×43, 646 = 2×17×19

Algorithm Analysis

The solution iterates through numbers, checking consecutive groups. For each number, it counts distinct prime factors by trial division.

It maintains a sliding window of four consecutive numbers, checking if all have exactly four distinct prime factors.

Performance Analysis

  • Time Complexity: O(n × √n) where n is the numbers checked
  • Space Complexity: O(1)
  • Execution Time: Moderate, finds answer in reasonable time
  • Scalability: Linear in search range, efficient for this problem

Key Insights

  • Numbers with many small prime factors are more likely to have multiple factors

  • The sequence starts at 134043 (134043, 134044, 134045, 134046)

  • Each has exactly four distinct prime factors

  • No smaller sequence exists

Educational Value

This problem teaches:

  • Prime factorization algorithms

  • Factor counting techniques

  • Searching for number sequences with specific properties

  • The distribution of numbers with given numbers of prime factors

Problem #47 is taken from Project Euler and licensed under CC BY-NC-SA 4.0.