Created
June 13, 2019 09:28
-
-
Save imechemi/e93e193cf92f9b8ddbb3489f9791b4f9 to your computer and use it in GitHub Desktop.
Object Oriented Prime Generator
This file contains hidden or 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 | |
def initialize() | |
@value = 2 | |
end | |
def value | |
@value | |
end | |
def next | |
i = @value | |
while true | |
i = i + 1 | |
break if is_prime?(i) | |
end | |
@value = i | |
@value | |
end | |
private | |
def is_prime?(n) | |
(2..n/2).to_a.each do |divisor| | |
return false if n % divisor == 0 | |
end | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment