fork download
  1. def sieve_of_eratosthenes(max)
  2. arr=(2..max).to_a
  3. (2..Math::sqrt(max)).each do |i|
  4. arr.delete_if {|a|a % i == 0 && a!=i}
  5. end
  6. arr
  7. end
  8.  
  9. # Example Call
  10. puts sieve_of_eratosthenes(20)
Success #stdin #stdout 0s 4760KB
stdin
Standard input is empty
stdout
2
3
5
7
11
13
17
19