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. What is the first of these numbers?


In [1]:
from euler import canonical_decomposition

In [2]:
d = canonical_decomposition(644)
d


Out[2]:
{2: 2, 7: 1, 23: 1}

In [3]:
len(d)


Out[3]:
3

In [4]:
from itertools import count

In [5]:
def foo(n):
    for i in count(2):
        for j in range(i, i + n):
            if len(canonical_decomposition(j)) != 4:
                break
        else:
            return i

In [6]:
n = 4
%timeit foo(n)
foo(n)


1 loops, best of 3: 2.97 s per loop
Out[6]:
134043

In [7]:
def foo(n):
    i = 2
    while True:
        for j in range(i, i + n):
            if len(canonical_decomposition(j)) != 4:
                i = j + 1
                break
        else:
            return i

In [8]:
n = 4
%timeit foo(n)
foo(n)


1 loops, best of 3: 2.51 s per loop
Out[8]:
134043