Skip to content

Instantly share code, notes, and snippets.

@alanhaugen
Last active December 10, 2017 22:25
Show Gist options
  • Save alanhaugen/61cdbd6c35b2d421da7488ea4041022e to your computer and use it in GitHub Desktop.
Save alanhaugen/61cdbd6c35b2d421da7488ea4041022e to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
def sieve(sand):
for i in sand:
for j in sand:
if j != i and j%i == 0:
sand.remove(j)
return sand
# recursion
def isPrime(number):
for i in range(2, number):
if number % i == 0:
return False
return True
# recursive function (calls itself)
def isPrimeD(number, d = None):
if d == None:
d = number
d = d - 1
if d <= 1:
return True
if number % d == 0:
return False
return isPrimeD(number, d)
print ("for range in i isPrime prime function")
print (isPrime(2))
print (isPrime(3))
print (isPrime(4))
print (isPrime(5))
print (isPrime(10))
print ("recursive function isPrimeD prime function")
print (isPrimeD(2))
print (isPrimeD(3))
print (isPrimeD(4))
print (isPrimeD(5))
print (isPrimeD(10))
print ("Sieve of Eratosthenes")
print ("Input (sand):")
numbers = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
print(*numbers, sep=' ')
print ("Output (primes):")
numbers = sieve(numbers)
print(*numbers, sep=' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment