-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem 7.py
More file actions
executable file
·41 lines (35 loc) · 1.36 KB
/
Problem 7.py
File metadata and controls
executable file
·41 lines (35 loc) · 1.36 KB
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
37
38
39
40
41
#!/usr/bin/env python
__author__ = 'Ned Udomkesmalee'
#What is the 10 001st prime number?
#Important facts: Primes > 3 will be of the form 6k +/- 1 and you only need to check for prime divisibility up to sqrt i
def problem7():
print("Find the nth prime number")
while True:
n = raw_input("What is n? ")
try:
n = int(n)
if n < 1:
print "*** n must be a positive non-zero integer ***"
else:
prime_list = [2, 3]
counter = 2
i = 6
while counter < n:
for next_num in [i-1, i+1]:
next_num_is_prime = True
upper_bound = next_num ** .5
for prime in prime_list:
if next_num % prime == 0:
next_num_is_prime = False
break
elif prime > upper_bound:
break
if next_num_is_prime:
prime_list.append(next_num)
counter += 1
i += 6
print "The %ith prime is %i" % (n, prime_list[n-1])
break
except ValueError:
print "*** %s is not a valid integer ***" % n
problem7()