Skip to content

Instantly share code, notes, and snippets.

@jjuliano
Created December 12, 2012 08:35
Show Gist options
  • Save jjuliano/4266127 to your computer and use it in GitHub Desktop.
Save jjuliano/4266127 to your computer and use it in GitHub Desktop.
Find the 10001st prime.
#!/usr/bin/env ruby
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
# What is the 10001st prime number?
# Solution:
primes, remainder = [], []
limit = 10001
rangemax = limit/10
n = 2
loop do
(2..rangemax).each do |x|
unless x==n
remainder << n%x
else
next
end
end
break unless primes[limit].nil?
primes << n unless remainder.include? 0
remainder=[]
n += 1
end
puts primes[limit-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment