-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem7.py
More file actions
48 lines (41 loc) · 874 Bytes
/
problem7.py
File metadata and controls
48 lines (41 loc) · 874 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
37
38
39
40
41
42
43
44
45
46
47
48
# Melissa Mangos
# Project Euler Problem 7
# What is the 10 001st prime number?
# Answer: 104743
# Checks if a value is prime
def is_prime(n):
# Divisible by 2 or 3
if n % 2 == 0 or n % 3 == 0:
return False
# Primes are in the form 6k +- 1
for i in range (5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
# Gets the nth prime number
def nth_prime(n):
# Base Cases
if (n == 1):
return 2
elif (n == 2):
return 3
# Already counting 2 & 3
count = 2
nth = 0
# Finds primes
k = 1
# Primes are in the form 6k +- 1
while (count < n):
# 6k - 1
if (is_prime((6*k) - 1)):
count += 1
nth = (6*k) - 1
# 6k + 1
if (is_prime((6*k) +1)):
# If 6k -1 happened to be the nth
if (count < n):
count += 1
nth = (6*k) + 1
k += 1
return nth
print("The 10001st prime is " + str(nth_prime(10001)))