Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created December 15, 2012 03:01
Show Gist options
  • Select an option

  • Save keithrbennett/4290991 to your computer and use it in GitHub Desktop.

Select an option

Save keithrbennett/4290991 to your computer and use it in GitHub Desktop.
Function to calculate prime numbers...uses !!!.
def prime_numbers(upper_bound)
return [] if upper_bound < 2
primes = [2]
3.upto(upper_bound) do |n|
is_prime = !!! primes.detect { |prime| mult_of?(n, prime) }
primes << n if is_prime
end
return primes
end
def mult_of?(n, factor)
n % factor == 0
end
@jjulian
Copy link

jjulian commented Dec 15, 2012

Line 5 could be simplified to is_prime = primes.none? { |prime| mult_of?(n, prime) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment