Skip to content

Commit 9377037

Browse files
committed
added method for nth prime
1 parent 70038a5 commit 9377037

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

exercism/nth_prime.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ def self.nth(count)
3535
raise ArgumentError if count < 1
3636
Prime.first(count).last
3737
end
38+
end
39+
40+
# Solution 3
41+
class Prime
42+
43+
# Credit for this solution at http://codereview.stackexchange.com/questions/90813/finding-the-nth-prime - this is not my code
44+
def self.nth n
45+
up_to = n * (Math.log(n) + 2)
46+
primes = (2..up_to).to_a
47+
primes.each {|num| primes.delete_if {|i| i > num && (i % num) == 0} }
48+
primes[n-1]
49+
end
3850
end

0 commit comments

Comments
 (0)