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?

Ruby: Running time = 0.0s
+#PrimeList

+#dig_split

def p49
  ps=[] 
  p=PrimeList.new
  p.getNext while p.last<1000
  ps.push(p.last)
  ps.push(p.getNext) while ps.last<10000
  ps.pop
  categories=Hash.new([])
  ps.each{|i|categories[dig_split(i).sort]+=[i]}
  categories.reject!{|k,v|v.length<3}
  categories.delete([1,4,7,8])
  while(categories.size>1)
    categories.each_pair do |k,v|
      unless v[2]-v[1]==v[1]-v[0]
        if(v.length>3)
          v.shift
	else
          categories[k]=nil
	end 
      end
      categories.reject!{|k,v|not v}
    end
  end
  puts categories.values.first.sort.join("")
end