1998-01-16 12:13:05 +00:00
|
|
|
# sieve of Eratosthenes
|
1999-01-20 04:59:39 +00:00
|
|
|
max = Integer(ARGV.shift || 100)
|
1999-08-13 05:45:20 +00:00
|
|
|
sieve = []
|
|
|
|
for i in 2 .. max
|
|
|
|
sieve[i] = i
|
|
|
|
end
|
1998-01-16 12:13:05 +00:00
|
|
|
|
1999-08-13 05:45:20 +00:00
|
|
|
for i in 2 .. Math.sqrt(max)
|
|
|
|
next unless sieve[i]
|
|
|
|
(i*i).step(max, i) do |j|
|
|
|
|
sieve[j] = nil
|
1998-01-16 12:13:05 +00:00
|
|
|
end
|
|
|
|
end
|
2002-06-28 14:42:46 +00:00
|
|
|
puts sieve.compact.join(", ")
|