mu chance or much chance ?

日々の戯れ言

Project Euler 47

  • 問題

Problem 47: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^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?

  • 解答例
import sympy

i = 2
while True:
    list1 = list(sympy.factorint(i).keys())
    list2 = list(sympy.factorint(i + 1).keys())
    list3 = list(sympy.factorint(i + 2).keys())
    list4 = list(sympy.factorint(i + 3).keys())

    temp1 = []
    temp1.extend(list1)
    temp1.extend(list2)
    temp1 = list(set(temp1))

    temp2 = []
    temp2.extend(list2)
    temp2.extend(list3)    
    temp2 = list(set(temp2))

    temp3 = []
    temp3.extend(list3)
    temp3.extend(list4)    
    temp3 = list(set(temp3))

    if len(list1) == 4 and len(list2) == 4 and len(list3) == 4 and len(temp1) == 8 and len(temp2) == 8 and len(temp3) == 8:
        print(i, i + 1, i + 2)
        break
    i += 1