home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rbemx144.zip / ruby-1.4.4 / sample / sieve.rb < prev    next >
Text File  |  1999-08-13  |  244b  |  15 lines

  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 ", "
  15.