Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Last active August 29, 2015 14:27
Show Gist options
  • Save kangkyu/1d622e93cd13d17b5f06 to your computer and use it in GitHub Desktop.
Save kangkyu/1d622e93cd13d17b5f06 to your computer and use it in GitHub Desktop.
# 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
@charliemcelfresh
Copy link

this is very slick -- well done!

Watch your spacing; number%factor should be number % factor

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