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?


Idea

Naive solution.

Iterate through and check the number of distinct prime factors.


In [1]:
from math import sqrt
from functools import reduce
from operator import mul
In [2]:
def get_prime_factors(n):
    factors = []
    d = 2
    while d <= sqrt(n):
        while n % d == 0:
            n //= d
            factors.append(d)
        d += 1
    if n != 1:
        factors.append(n)
    return factors
In [3]:
get_prime_factors(8)
Out[3]:
[2, 2, 2]
In [4]:
get_prime_factors(12)
Out[4]:
[2, 2, 3]
In [5]:
get_prime_factors(13)
Out[5]:
[13]
In [6]:
get_prime_factors(644)
Out[6]:
[2, 2, 7, 23]
In [7]:
def solve(consecutive_number, distinct_prime_factors_number):
    first_number = reduce(mul, ([2, 3, 5, 7, 11])[:distinct_prime_factors_number], 1)
    while True:
        for i, n in enumerate(range(first_number, first_number+consecutive_number), 1):
            if len(set(get_prime_factors(n))) != distinct_prime_factors_number:
                first_number += i
                break
            if i == consecutive_number:
                return first_number
In [8]:
solve(2, 2)
Out[8]:
14
In [9]:
solve(3, 3)
Out[9]:
644
In [10]:
solve(4, 4)
Out[10]:
134043