Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View ruby_prime.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!/usr/bin/env ruby
#
def help
puts "#{$0} <find_primes_to>"
end
 
def find_primes(array_list)
array_list.each do |num|
i = num
if num * i > array_list.last
break
else
while num * i <= array_list.last
array_list.delete(num * i)
i += 1
end
end
end
return array_list
end
 
begin
unless ARGV.length == 1
help
exit(1)
end
prime_to = ARGV[0]
array_list = []
prime_to = prime_to.to_i + 1
prime_to.times {|d| array_list << d}
[0,1].each {|n| array_list.delete(n) }
results = find_primes(array_list)
print "#{results}\n"
rescue => e
puts "#{e}"
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.