require 'prime'
BASE = 3
MAX = 500
stems = Prime.each(BASE-1).to_a
(1..MAX-1).each {|i|
  print 
  t = []
  b = BASE ** i
  stems.each {|z|
    (1..BASE-1).each {|n|
      c = n*b+z
      t.push(c) if c.prime?
  }}
  break if t.empty?
  stems = t
}
puts less than