forked from rmurphey/project-euler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p007.rb
49 lines (37 loc) · 745 Bytes
/
p007.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
38
39
40
41
42
43
44
45
46
47
48
49
# 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 10 001st prime number?
class NthPrime
attr_reader :primes
def initialize(nth)
@count = 1;
@primes = [ 2 ]
while @primes.length < nth
@primes << find_next_prime
end
end
def answer
@primes.last
end
def find_next_prime
found = false
next_prime = @primes.last
while !found
next_prime += 1
found = is_prime(next_prime)
end
next_prime
end
def is_prime(num)
prime = true
@primes.each do |p|
if num % p == 0
prime = false
break
end
end
prime
end
end
solution = NthPrime.new(10001)
puts solution.answer