Project Euler Problem 49
問題
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence?
ソース
max = 10000 prime = Array.new(max, 1) prime[0..1] = [0, 0] i = 2 while i < Math.sqrt(max).to_i + 1 do (i + i).step(max, i){|x| prime[x] = 0} i += prime[(i + 1)..max].index(1) + 1 end prime.each_index{|i| prime[i] = i unless prime[i].zero? }.select{|x| !x.zero? && x >= 1000}.each{|x| array = x.to_s.scan(/./).permutation(4).map{|a| a.join.to_i}.uniq.sort.select{|y| y >= 1000 && !prime[y].zero? } next if array[0] != x next if array.size < 3 array.each_index{|i| (1..(array.size - i - 2)).each{|a| add = array[i + a] - array[i] if array.find{|y| y == array[i] + add} && array.find{|y| y == array[i] + add + add} puts "#{array[i]}#{array[i] + add}#{array[i] + add + add}" break end } } }
解答
296962999629
感想
実はこれ、少しハマりました。。。
ソース汚い
とりあえず解けて満足です