<jupyter_start><jupyter_text>[Project Euler](https://projecteuler.net/) Distinct primes factors[Problem 47](https://projecteuler.net/problem=47) The first two consecutive numbers to have two distinct prime factors are:  > 14 = 2 × 7  > 15 = 3 × 5The 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?<jupyter_code>from euler import canonical_decomposition
d = canonical_decomposition(644)
d
len(d)
from itertools import count
def foo(n):
    for i in count(2):
        for j in range(i, i + n):
            if len(canonical_decomposition(j)) != n:
                break
        else:
            return i
n = 4
%timeit foo(n)
foo(n)
from functools import lru_cache
@lru_cache(n)
def n_distinct_prime_factors(x):
    return len(canonical_decomposition(x))
def foo(n):
    for i in count(2):
        for j in range(i, i + n):
            if n_distinct_prime_factors(j) != n:
                break
        else:
            return i
n = 4
%timeit foo(n)
foo(n)
def foo(n):
    i = 2
    while True:
        for j in range(i, i + n):
            if len(canonical_decomposition(j)) != n:
                i = j + 1
                break
        else:
            return i
n = 4
%timeit foo(n)
foo(n)
i = 2
list(range(i-1, i-n-1, -1))
def foo(n):
    i = 2
    while True:
        if n_distinct_prime_factors(i) != n:
            i += n
            continue

        # Search backwards.
        for j in range(i-1, i-n-1, -1):
            if j < 2 or n_distinct_prime_factors(j) != n:
                first_good = j + 1
                break
        else:
            first_good = j

        # Search forwards.
        for j in range(i+1, first_good+n):
            if n_distinct_prime_factors(j) != n:
                i = j + n
                break
        else:
            return i
n = 4
%timeit foo(n)
foo(n)<jupyter_output>1 loop, best of 3: 479 ms per loop
{'fraction_non_alphanumeric': 0.10068093385214008, 'fraction_numerical': 0.029669260700389104, 'mean_word_length': 1.9135977337110481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '603534', 'license': 'MIT', 'n_tokens_mistral': 749, 'n_tokens_neox': 672, 'n_words': 236, 'path': 'euler-047-distinct-primes-factors-20170110.ipynb', 'repo_name': 'james-prior/euler', 'size': '2056'}