Skip to content

Instantly share code, notes, and snippets.

@KevinWMatthews
Created March 25, 2015 02:56
Show Gist options
  • Save KevinWMatthews/95532e60ffe1dd714e2a to your computer and use it in GitHub Desktop.
Save KevinWMatthews/95532e60ffe1dd714e2a to your computer and use it in GitHub Desktop.
The entire solution
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