Skip to content

Instantly share code, notes, and snippets.

@gavinmyers
Created May 14, 2012 02:11
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 gavinmyers/2691317 to your computer and use it in GitHub Desktop.
Save gavinmyers/2691317 to your computer and use it in GitHub Desktop.
Prime Number Calculator in Ruby
#!/usr/bin/env ruby
puts "limit number:"
max = Integer(gets)
primes = []
puts "going from 2 to #{max}"
(2..max).select do |current|
# if current is divisible by any number in the array (that is not itself) it is not prime
isPrime = true
primes.each do |prime|
md = current % prime
if md == 0 then isPrime = false end
end
if isPrime then primes << current end
end
primes.each do |prime|
puts prime
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment