Skip to content

Instantly share code, notes, and snippets.

@dehli
Created July 1, 2015 21:33
Show Gist options
  • Save dehli/2af9d8f6560f78ae9743 to your computer and use it in GitHub Desktop.
Save dehli/2af9d8f6560f78ae9743 to your computer and use it in GitHub Desktop.
Primes
# 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