-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblem10.py
More file actions
26 lines (23 loc) · 804 Bytes
/
Problem10.py
File metadata and controls
26 lines (23 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def ifPrime(n):
''' returns true/false w.r.t primality of a number.
From wikipedia, we find that all primes are of the form 6k +/- 1
So, instead of doing it till sq(n), lets check, 2, 3 and then every number of the form 6k +/- 1
'''
#print math.sqrt(n)
sqrt = math.sqrt(n)
if n == 2 or n == 3 or n == 5:
return True
if n % 2 == 0 or n % 3 == 0:
return False
if str(n).endswith('0') or str(n).endswith('5'):
return False
k = 1
first, second = (6 * k) - 1, (6 * k) + 1
while first <= sqrt:
if n%first == 0 or n%second == 0:
return False
k += 1
first, second = (6 * k) - 1, (6 * k) + 1
return True
start = 2
print start + sum([n for n in xrange(3, 2000000, 2) if ifPrime(n)])