Created
September 21, 2015 06:58
-
-
Save joelibaceta/f1ff812b73d4a7c3c7d9 to your computer and use it in GitHub Desktop.
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
# Implement a Primes class with a class method first(n) | |
# that returns an array of the first n prime numbers. | |
class Primes | |
def self.first(total, primes=[2], n=2) | |
primes.count >= total ? primes : (eval ((2..n-1).map{|j| (n % j == 0) ? false : true}).join(" && ")) ? first(total, primes.push(n), n+1) : first(total, primes, n+1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment