Skip to content

Instantly share code, notes, and snippets.

@xjunior
Created April 11, 2011 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjunior/914146 to your computer and use it in GitHub Desktop.
Save xjunior/914146 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes is an algorithm to find prime numbers from 2 to a given number.
## Code
module Crivo
def self.gera_primos(til)
list = (2..til).to_a
list.each do |pivot|
list = remove_multiples(list, pivot)
end
list
end
def self.remove_multiples(list, pivot)
list.find_all {|n| (n % pivot) != 0 || pivot.eql?(n) }
end
end
puts Crivo.gera_primos(100)
## Output
$ ruby crivo_eratostenes.rb
97
89
83
79
73
71
67
61
59
53
47
43
41
37
31
29
23
19
17
13
11
7
5
3
2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment