#Based on http://rosettacode.org/wiki/Prime_decomposition
# Get prime decomposition of integer _i_.
# This routine is more efficient than prime_factors,
# and quite similar to Integer#prime_division of MRI 1.9.
def prime_factors(i)
  factors = []
  check = proc do |p|
    while(q, r = i.divmod(p)
          r.zero?)
      factors << p
      i = q
    end
  end
  check[2]
  check[3]
  p = 5
  while p * p <= i
    check[p]
    p += 2
    check[p]
    p += 4    # skip multiples of 2 and 3
  end
  factors << i if i > 1
  factors
end

def mpf(n)
    v=prime_factors(n); s=((v.length)/2.0).ceil
    r=v[s-1]
    r
end

def get_next(v)
    j=1
    v.each {|i| j *= i}
    mpf(1+j)    
end

def first(m)
    v=[2]; t=m-1
    t.times do v << get_next(v) end
    v
end

def firstm(m)
v=[]
(2..m).to_a.each {|n| v << mpf(n)}
v
end

p first(9); print("\n")

#9=>[2, 3, 7, 43, 13, 53, 5, 6221671, 38709183810571]