sample/sieve.rb


DEFINITIONS

This source file includes following functions.


   1  # sieve of Eratosthenes
   2  max = Integer(ARGV.shift || 100)
   3  sieve = []
   4  for i in 2 .. max
   5    sieve[i] = i
   6  end
   7  
   8  for i in 2 .. Math.sqrt(max)
   9    next unless sieve[i]
  10    (i*i).step(max, i) do |j|
  11      sieve[j] = nil
  12    end
  13  end
  14  puts sieve.compact.join(", ")