Project Euler Problem 47

問題

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

ソース

require 'mathn'

n = 4
pd = (1...1 + n).map{|i| i.prime_division}

(1 + n).upto(10 ** 6){|i|
	pd.shift
	pd << i.prime_division
	all = pd.inject([]){|a, x| a + x}
	if pd.all?{|x| x.size == n} && all.size == all.uniq.size
		puts i - n + 1
		break
	end
}

解答

134043

感想

全ての[素因数, 乗数]の組を配列として、uniqすると全てが異なる素因数かが判断できます。
これもそんなに難しくない