def prime?(n)                     
  return n | 1 == 3 if n < 5      
  return false if n.gcd(6)!= 1   
  sqrtN = Integer.sqrt(n)
  pc = -1                         
  until (pc += 6) > sqrtN         
    return false if n % pc == 0 || n % (pc + 2) == 0  
  end
  true
end

def gen_primes(a, b)
    (a..b).select { |pc| pc if prime? pc }
end

def nsmooth(n, limit)
    raise  if n < 2 || n > 521 || limit < 1
    raise  unless prime? n

    primes = gen_primes(2, n)
    ns = [0] * limit
    ns[0] = 1
    nextp = primes[0..primes.index(n)]

    indices = [0] * nextp.size
    (1...limit).each do |m|
        ns[m] = nextp.min
        (0...indices.size).each do |i|
            if ns[m] == nextp[i]
                indices[i] += 1
                nextp[i] = primes[i] * ns[indices[i]]
            end
        end
    end
    ns
end

gen_primes(2, 29).each do |prime|
    print 
    print nsmooth(prime, 25)
    puts
end
puts
gen_primes(3, 29).each do |prime|
    print 
    print nsmooth(prime, 3002)[2999..-1]    
    puts
end
puts
gen_primes(503, 521).each do |prime|
    print 
    print nsmooth(prime, 30019)[29999..-1]  
    puts
end