Create a gist now

Instantly share code, notes, and snippets.

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