Created
April 14, 2014 06:15
-
-
Save sota1235/10620698 to your computer and use it in GitHub Desktop.
エラトステネスの篩を頑張って高速化する
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
#!/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