Skip to content

Instantly share code, notes, and snippets.

@sota1235
Created April 14, 2014 06:15
Show Gist options
  • Save sota1235/10620698 to your computer and use it in GitHub Desktop.
Save sota1235/10620698 to your computer and use it in GitHub Desktop.
エラトステネスの篩を頑張って高速化する
#!/usr/bin/python
import math
def sieve(n):
nums = [i+1 for i in range(2, n, 2) if (i+1) % 3 != 0 and (i+1) % 5 !=0]
ans = [2,3,5]
while nums[0] <= math.sqrt(n):
for i in range(nums[0]**2, nums[-1]+1, nums[0]):
if i in nums: nums.remove(i)
ans.append(nums.pop(0))
ans += nums
return ans
print sieve(10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment