Created
February 6, 2014 17:33
-
-
Save gr33ndata/8848854 to your computer and use it in GitHub Desktop.
Prime numbers generator!
This file contains hidden or 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
# Using Sieve of Eratosthenes | |
# https://en.wikipedia.org/wiki/Generating_primes | |
import sys | |
def main(): | |
num = int(sys.argv[1]) | |
num_list = [[i,True] for i in range(num)] | |
num_list[0][1] = False | |
num_list[1][1] = False | |
prime_list = [] | |
for i in range(num): | |
if num_list[i][1] == True: | |
prime_list.append(num_list[i][0]) | |
for j in range(2, num/num_list[i][0] + 1): | |
k = j * num_list[i][0] | |
if k < num: | |
#print i, j, k, num_list[k] | |
num_list[k][1] = False | |
return prime_list | |
if __name__ == '__main__': | |
x = main() | |
print x[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment