mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-25 04:30:15 +00:00
7f04e5cd34
* spelling corrections * review * improved documentation, removed redundant variables, added testing * added type hint * camel case to snake case * spelling fix * review * python --> Python # it is a brand name, not a snake * explicit cast to int * spaces in int list * "!= None" to "is not None" * Update comb_sort.py * various spelling corrections in documentation & several variables naming conventions fix * + char in file name * import dependency - bug fix Co-authored-by: John Law <johnlaw.po@gmail.com>
55 lines
906 B
Python
55 lines
906 B
Python
"""
|
|
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 Nth prime number?
|
|
"""
|
|
from math import sqrt
|
|
|
|
|
|
def is_prime(n):
|
|
if n == 2:
|
|
return True
|
|
elif n % 2 == 0:
|
|
return False
|
|
else:
|
|
sq = int(sqrt(n)) + 1
|
|
for i in range(3, sq, 2):
|
|
if n % i == 0:
|
|
return False
|
|
return True
|
|
|
|
|
|
def solution(n):
|
|
"""Returns the n-th prime number.
|
|
|
|
>>> solution(6)
|
|
13
|
|
>>> solution(1)
|
|
2
|
|
>>> solution(3)
|
|
5
|
|
>>> solution(20)
|
|
71
|
|
>>> solution(50)
|
|
229
|
|
>>> solution(100)
|
|
541
|
|
"""
|
|
i = 0
|
|
j = 1
|
|
while i != n and j < 3:
|
|
j += 1
|
|
if is_prime(j):
|
|
i += 1
|
|
while i != n:
|
|
j += 2
|
|
if is_prime(j):
|
|
i += 1
|
|
return j
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(solution(int(input().strip())))
|