Created
May 22, 2016 06:58
-
-
Save hbhargava7/ddebdd2d764739017ca951adea29168b to your computer and use it in GitHub Desktop.
Basic, concise primes sieve in Python.
This file contains 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
# 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