Created
July 1, 2015 21:33
-
-
Save dehli/2af9d8f6560f78ae9743 to your computer and use it in GitHub Desktop.
Primes
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
# This function finds all the prime numbers that are less than a certain value | |
# getPrimes(10) would return [2, 3, 5, 7] | |
def getPrimes (n): | |
primes = [] | |
for i in range(2, n): | |
isPrime = True | |
for primeVal in primes: | |
# Has a factor | |
if i % primeVal == 0: | |
isPrime = False | |
break | |
# Once you've reached the sqrt of i, it cannot have any higher factors | |
elif primeVal > i ** 0.5: | |
break | |
if isPrime: | |
primes.append(i) | |
return primes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment