Created
December 29, 2011 12:26
-
-
Save marsbomber/1533852 to your computer and use it in GitHub Desktop.
Prime numbers
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 | |
def run max | |
return unless max > 1 | |
return (2 .. max).inject([]) do |r, i| | |
if i.even? | |
r << i if i == 2 | |
else | |
composite = false | |
(3 .. Math.sqrt(i)).each do |t| | |
if i % t == 0 | |
composite = true | |
break | |
end | |
end | |
r << i unless composite | |
end | |
r | |
end | |
end | |
max = Integer(ARGV.shift) | |
puts run(max).join(' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment