Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Created September 21, 2015 06:58
Show Gist options
  • Save joelibaceta/f1ff812b73d4a7c3c7d9 to your computer and use it in GitHub Desktop.
Save joelibaceta/f1ff812b73d4a7c3c7d9 to your computer and use it in GitHub Desktop.
# 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