-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeCalcFast.py
More file actions
43 lines (31 loc) · 979 Bytes
/
PrimeCalcFast.py
File metadata and controls
43 lines (31 loc) · 979 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
from math import sqrt
from time import perf_counter
calcTo: int = int(input("Calculate the nth prime:"))
time1 = perf_counter()
def prime(num: int) -> bool:
"""
Returns a bool representing the primality of the argument
:param int num: a positive integer that follows the pattern 6n+/-1
:returns: True if num is prime, False otherwise
:rtype: bool
"""
for div in range(5, int(sqrt(num))+1, 6):
if num % div == 0 or num % (div+2) == 0:
return False
return True
def main():
primes_found: int = 2
current_num: int = 1
while primes_found < calcTo:
current_num += 4
primes_found += prime(current_num)
if primes_found >= calcTo:
return current_num
current_num += 2
primes_found += prime(current_num)
return current_num
if __name__ == "__main__":
num = main()
time2 = perf_counter()
print(f'Your #: {num} \
\nTime: {time2-time1}sec')