Problem 49

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?

[code=ruby]def eratostenes(n)
sito = Array.new(n, true)
sito[0],sito[1],sito[2] = nil,nil,true
i = 3
while i <= n do
unless sito[i]==nil then
j = 2*i
while j <= n
sito[j] = nil
j += i
end
end
i += 2
end
return [2] + (3…n).select{|i| sito[i] == true && i.odd?}
end

primes = eratostenes(10000)-eratostenes(1000)
list = Hash.new
primes.each do |prime|
tmp = prime.to_s.split(’’).sort.join
list[tmp] = Array.new if list[tmp].nil?
list[tmp] << prime
end
tmp = Array.new(list.size/3)
list = list.map{|k,v| v.sort}.select{|t| t.size>2}.map{|t| t.combination(3).to_a}.flatten
(1…(list.size/3)).each do |i|
tmp[i-1] = Array.new
3.times do
tmp[i-1] << list.shift
end
end
p tmp.select{|d| d[2]-d[1]==d[1]-d[0]}.last.join[/code]

[code=ruby]require “prime”

p (1000…9997).map {|x|
[x, (1…4500).select {|r|
a = [x, x+r, x+r+r]
a.all?{|e| e.prime?} && a.map {|e| e.to_s.split(’’).sort}.uniq.size == 1
}]
}.select {|e| e.last.size > 0}.flatten[/code]