Skip to content

Instantly share code, notes, and snippets.

@syohex
Created November 15, 2012 00:39
Show Gist options
  • Save syohex/4075868 to your computer and use it in GitHub Desktop.
Save syohex/4075868 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
#!/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