-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem007.py
More file actions
68 lines (54 loc) · 1.28 KB
/
problem007.py
File metadata and controls
68 lines (54 loc) · 1.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
from math import isqrt
def isPrime(n):
if n == 1:
return False
if n <= 3:
return True
# only need to check up to sqrt(n)
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
# Faster prime test
# - only test with known primes
# - stop at sqrt(n)
primesKnown = [2, 3]
def isPrimeFast(n):
if n == 1:
return False
if n <= 3:
return True
for p in primesKnown:
if p > (isqrt(n)+1):
break
if n % p == 0:
return False
primesKnown.append(n)
return True
start = time.perf_counter()
candidate = 3
n = 1
N = 10001
print(f"Calculating the {N}-th prime number")
print("\n--- Running isPrime() ---")
while n < N:
if isPrime(candidate):
n = n + 1
print(f"{n}-th prime is {candidate}".ljust(40),end='\r')
candidate += 2
print()
end = time.perf_counter()
print(f"Elapsed time: {end - start:.6f} seconds")
print("\n--- Running isPrimeFast() ---")
start = time.perf_counter()
candidate = 3
n = 1
while n < N:
if isPrimeFast(candidate):
n = n + 1
print(f"{n}-th prime is {candidate}".ljust(40),end='\r')
candidate += 2
print()
end = time.perf_counter()
print(f"Elapsed time: {end - start:.6f} seconds")