Created
May 14, 2012 02:11
-
-
Save gavinmyers/2691317 to your computer and use it in GitHub Desktop.
Prime Number Calculator in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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