def pn(max) m = (max + 1) / 2 $pn = Array.new(max) for i in 1..m $pn[i] = (i << 1) - 1 end n = m + 1 for p in 2..m next unless $pn[p] q = p + $pn[p] while q < n $pn[q] = nil q += $pn[p] end end $pn.compact! $pn[0] = 2 return $pn end t1=Time.now pn(1_000_000) t2=Time.now p $pn[0..23] p t2-t1 loop{eval(gets) rescue p $!}
This method is probably the most efficiency method.
To list the prime numbers that less than 1 million just use 2.532s.


Good work! Hard work?