Help us understand the problem. What is going on with this article?

Project Euler 47「異なる素因数」

More than 1 year has passed since last update.

クッソ遅い。死ぬほど遅い。
20分強かかります。。。

Problem 47 「異なる素因数」

それぞれ2つの異なる素因数を持つ連続する2つの数が最初に現れるのは:

14 = 2 × 7
15 = 3 × 5

それぞれ3つの異なる素因数を持つ連続する3つの数が最初に現れるのは:

644 = 2^2 × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19

最初に現れるそれぞれ4つの異なる素因数を持つ連続する4つの数を求めよ. その最初の数はいくつか?

from itertools import count

def hoge(target):
    cnt = 0
    for n in count(1):
        if not is_prime(n) and is_four_prime_factors(n, target):
            cnt += 1
            if cnt == target: return n - (target - 1)
        else:
            cnt = 0

def is_four_prime_factors(n, target):
    primes = set()
    for m in get_prime(n):
        if n == 1 or len(primes) > target: break
        while True:
            if n % m == 0:
                primes.add(m)
                n = n / m
            else:
                break
    return True if len(primes) == target else False

def get_prime(n):
    yield 2
    for m in range(3, n // 2 +1):
        if is_prime(m): yield m

def is_prime(n):
    if n < 2: return False
    if n == 2: return True
    if n % 2 == 0: return False
    return all(n % i != 0 for i in range(3, int(n ** 0.5) + 1, 2))

print(hoge(4))

どうやったら早くなるんや。。。

yopya
自然に囲まれた田舎で働きたい。 田舎でPythonの仕事ないっすか?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away