-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe7.py
More file actions
50 lines (45 loc) · 1.25 KB
/
pe7.py
File metadata and controls
50 lines (45 loc) · 1.25 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
42
43
44
45
46
47
48
49
50
############################################################################################################################
#
# Problem 7
#
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
#
# What is the 10001st prime number?
#
# Ans: 104743
############################################################################################################################
def isPrime(num: int) -> bool:
'''
Args:
num (int): number to check if prime
Returns:
(bool): True if prime, false if not
'''
if num == 1:
return False
elif num == 2:
return True
elif num % 2 == 0:
return False
else:
# Check if i divides the first half of num
for i in range(2, num//2+1):
if num % i == 0:
return False
return True
def nPrimes(n: int) -> list:
'''
Args:
n (int > 0): The index of the prime number to return
Returns:
primes (List[int]): A list of the first n primes
'''
i = 2
primes = []
while len(primes) <= n-1:
if isPrime(i):
primes.append(i)
i += 1
return primes
if __name__ == "__main__":
print(nPrimes(10001)[-1])