Created
December 12, 2012 08:35
-
-
Save jjuliano/4266127 to your computer and use it in GitHub Desktop.
Find the 10001st prime.
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 | |
# 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