Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # the sieve of Eratosthenes
- # import math for sqrt()
- from math import sqrt
- def theSieve(maxNum):
- # create a list w/o 1
- searchList = list(range(2, maxNum + 1))
- # create a list to store prime numbers
- primeList = []
- # begin the sieve process
- while True:
- # if the first number of searchList is bigger than sqrt(maxNum),
- # transfer all the rest numbers from searchList into primeList
- if searchList[0] >= sqrt(maxNum):
- for n in range(len(searchList)):
- primeList.append(searchList.pop(0))
- # check if searchList is empty
- if len(searchList) == 0:
- return primeList
- # transfer the first number from searchList into primeList
- primeList.append(searchList.pop(0))
- # delete the multiples of the transfered number
- for n in searchList:
- if n % primeList[-1] == 0:
- searchList.remove(n)
- print(theSieve(61))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement