-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblem7.py
More file actions
36 lines (32 loc) · 923 Bytes
/
Problem7.py
File metadata and controls
36 lines (32 loc) · 923 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
27
28
29
30
31
32
33
34
35
36
#find the 10001st prime number
import math
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
'''
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
num_primes = 6
start = 17
while True:
if num_primes == 10001:
print start - 2
break
if ifPrime(start):
num_primes += 1
start += 2