Created
December 15, 2012 03:01
-
-
Save keithrbennett/4290991 to your computer and use it in GitHub Desktop.
Function to calculate prime numbers...uses !!!.
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
| 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 5 could be simplified to
is_prime = primes.none? { |prime| mult_of?(n, prime) }