Created
November 15, 2012 00:39
-
-
Save syohex/4075868 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
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
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import sys | |
def eratos(n): | |
nums = dict([(x, 1) for x in range(2, n)]) | |
for x in range(2, n): | |
i = 2 | |
while (x * i) < n: | |
if x * i in nums: | |
del(nums[x * i]) | |
i += 1 | |
return nums.keys() | |
if __name__ == "__main__": | |
num = 100 | |
if len(sys.argv) > 2: | |
num = int(sys.argv[1]) | |
print(eratos(num)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment