Last active
August 29, 2015 14:27
-
-
Save kangkyu/1d622e93cd13d17b5f06 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
# write a function called find_primes, where you pass in an integer (like 100), | |
# and your function will calculate all the prime numbers from 1 - 100 | |
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
def find_primes(integer) | |
current_ary = (2..integer).to_a | |
current_idx = 0 | |
while current_idx < current_ary.length | |
current_ary.delete_if do |e| | |
current_ary[(current_idx + 1)..-1].member?(e) && | |
e % current_ary[current_idx] == 0 | |
end | |
current_idx += 1 | |
end | |
current_ary | |
end | |
find_primes(100) | |
# puts find_primes(100).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is very slick -- well done!
Watch your spacing; number%factor should be number % factor