Skip to content

Instantly share code, notes, and snippets.

@hbhargava7
Created May 22, 2016 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbhargava7/ddebdd2d764739017ca951adea29168b to your computer and use it in GitHub Desktop.
Save hbhargava7/ddebdd2d764739017ca951adea29168b to your computer and use it in GitHub Desktop.
Basic, concise primes sieve in Python.
# Created by Hersh Bhargava of H2 Micro (www.h2micro.com)
#sieve for primes between 1 and cap.
def prime_sieve(cap):
n = cap + 1
nprime = set()
prime = []
for i in range(2, n):
print("Sieve Operation Completion: " + str((i/n) * 100))
if i in nprime:
continue
for f in range(i*2, n, i):
nprime.add(f)
prime.append(i)
return prime
# if you wanted to find the nth prime
def find_primes(array):
start_time = time.time()
maximum = max(array)
n = 2
# warning - low quality estimation protocol
while len(prime_sieve(n)) < maximum:
n += 100000
primeArray = prime_sieve(n)
result = []
for i in array:
result.append(primeArray[i])
print(result)
print("--- Sieve Operation Completed in: %s seconds ---" % (time.time() - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment