Skip to content

Instantly share code, notes, and snippets.

@hitsumabushi
Created August 1, 2012 15:34
Show Gist options
  • Save hitsumabushi/3227917 to your computer and use it in GitHub Desktop.
Save hitsumabushi/3227917 to your computer and use it in GitHub Desktop.
Dayly programing (Project Euler:#10)
#/usr/bin/python
import math
def is_prime(x):
if x < 0:
x = -x
if x < 2:
return False
# check the case: x=2,3
if x % 2 == 0:
return x == 2
elif x % 3 == 0:
return x == 3
root = int(math.floor(math.sqrt(x))) + 1
# Prime = 6k-1 or 6k+1
pri = 5
height = 2
while pri <= root:
if x % pri == 0:
return False
pri = pri + height
height = 6 - height
return True
def sum_prime(x):
if x < 0:
x = -x
if x < 2:
return 0
result = 0
if x >= 2:
result = 2
for s in range(3,x+1,2):
if is_prime(s):
result = result + s
return result
if __name__ == "__main__":
import sys
print(sum_prime(int(sys.argv[1])))
#=> 142913828922
#=> time: 8.70s ( with corei7, mem 16G)
@hitsumabushi
Copy link
Author

他の人のを見てて気づいたけど、こういうので引数として、問題に与えた数を処理するのは良くないのかー。ミスった。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment