Created
March 25, 2015 02:56
-
-
Save KevinWMatthews/95532e60ffe1dd714e2a to your computer and use it in GitHub Desktop.
The entire solution
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
class Prime | |
include Enumerable | |
def self.nth(index) | |
raise ArgumentError, 'index out of bound' if index <= 0 | |
new.take(index).last | |
end | |
def each(&block) | |
(2..Float::INFINITY).each do |i| | |
yield(i) if prime?(i) | |
end | |
end | |
def prime?(i) | |
(2..Math.sqrt(i).to_i).none? { |divisor| i % divisor == 0 } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment