Skip to content

Instantly share code, notes, and snippets.

@marsbomber
Created December 29, 2011 12:26
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 marsbomber/1533852 to your computer and use it in GitHub Desktop.
Save marsbomber/1533852 to your computer and use it in GitHub Desktop.
Prime numbers
#!/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