Project

General

Profile

nth_prime.rb

Breaking code - claytonflesher (Clayton Flesher), 01/05/2016 06:59 PM

 
1
class Prime
2
  def self.nth(num)
3
    error(num)
4
    primes = build_primes(num)
5
    primes.last
6
  end
7

    
8
  private
9

    
10
  def build_primes(num)
11
    primes = [2]
12
    index  = 3 
13
    until primes.size == num
14
      if example?(index, primes)
15
        primes << index
16
      end
17
      index += 1
18
    end
19
    primes
20
  end
21

    
22
  def error(num)
23
    unless num > 0
24
      raise ArgumentError
25
    end
26
  end
27

    
28
  def example?(index, primes)
29
    primes.each do |prime|
30
      if index % prime == 0
31
        return false
32
      end
33
    end
34
    true
35
  end
36
end