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?
Naive solution.
Iterate through and check the number of distinct prime factors.
from math import sqrt
from functools import reduce
from operator import mul
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
get_prime_factors(8)
get_prime_factors(12)
get_prime_factors(13)
get_prime_factors(644)
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
solve(2, 2)
solve(3, 3)
solve(4, 4)