Created
March 9, 2017 01:01
-
-
Save cdlewis/4d5d268e14b08f278b7c05b259ab4301 to your computer and use it in GitHub Desktop.
jib.py
This file contains 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
# getPrimes takes one argument, maxNum, and returns all prime numbers | |
# that exist between 2 and maxNum using the sieve of erasmus algorithm | |
def getPrimes(maxNum): | |
primes = [] | |
# create a list of all possible prime numbers between 2 and maxNum | |
candidates = [i for i in xrange(2, maxNum)] | |
# while there are possible prime numbers remaining | |
while len(candidates) > 0: | |
# take the first number from the candidates list. This must be | |
# a prime number. If this is the first time through the loop | |
# then it's 2 and 2 is a prime number. If it is a subsequent | |
# run through the loop then it must be a prime number of else | |
# it would have been removed by our algorithm. | |
new_prime = candidates.pop(0) | |
# add the new prime to the results list | |
primes.append(new_prime) | |
# Remove all numbers from the candidates list that are divisible | |
# by new_prime. A number that is disible by a prime number cannot | |
# itself be a prime number | |
candidates = [i for i in candidates if i % new_prime != 0] | |
return primes | |
print getPrimes(20000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment