Class: Prime
- Inherits:
-
Object
- Object
- Prime
- Defined in:
- lib/hack01/nth_prime.rb
Class Method Summary collapse
Class Method Details
.is_prime(number_to_test) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/hack01/nth_prime.rb', line 5 def is_prime number_to_test prime = true number_to_test arr = (2 .. (number_to_test - 1) ).to_a arr.each do |n| if (number_to_test % n) == 0 prime = false end end return prime end |
.nth(num) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hack01/nth_prime.rb', line 19 def nth num throw ArgumentError.new unless num > 0 primes = [2] candidate = primes.last while primes.length < num candidate = candidate + 1 if is_prime candidate primes.push candidate end end # p primes primes.last end |