-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnth_prime.py
38 lines (30 loc) · 894 Bytes
/
nth_prime.py
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
from collections import OrderedDict
import math
# Keed track of previously asked primes through memoization:
def prime_finder():
primes = OrderedDict({1: 2})
max_n = 1
x = 3
def nth_prime_finder(n):
nonlocal max_n, x
raise_if_non_positive(n)
if n <= max_n:
return primes[n]
max_n = n
while len(primes) < n:
if not has_factor_in(x, primes):
primes[len(primes) + 1] = x
x += 2
return primes[n]
return nth_prime_finder
def has_factor_in(x, candidates):
for f in candidates.values():
if f > math.sqrt(x):
return False
if x % f == 0:
return True
return False
def raise_if_non_positive(n):
if not isinstance(n, int) or n < 1:
raise ValueError(f"n must be a positive integer, not {n}.")
nth_prime = prime_finder()