-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (48 loc) · 1.81 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys
import os
import time
from src.aks import normal_aks_primality_test, fft_aks_primality_test
def run_tests_from_file(file_path):
if not os.path.exists(file_path):
print(f"Error: The file '{file_path}' does not exist.")
return
with open(file_path, 'r') as f:
lines = f.readlines()
header = ("Number", "Normal AKS", "FFT AKS", "Normal Time (s)", "FFT Time (s)", "Improvement (%)")
print("{:<10} {:<12} {:<10} {:<18} {:<14} {:<16}".format(*header))
print("-" * 80)
for line in lines:
line = line.strip()
if not line:
continue
try:
n = int(line)
except ValueError:
print(f"Skipping non-numeric line: {line}")
continue
# Measure time for normal AKS test
start_normal = time.perf_counter()
res_normal = normal_aks_primality_test(n)
end_normal = time.perf_counter()
normal_time = end_normal - start_normal
# Measure time for FFT-optimized AKS test
start_fft = time.perf_counter()
res_fft = fft_aks_primality_test(n)
end_fft = time.perf_counter()
fft_time = end_fft - start_fft
# Calculate improvement percentage as the absolute value (always positive)
improvement = abs((normal_time - fft_time) / normal_time * 100) if normal_time > 0 else 0
print("{:<10} {:<12} {:<10} {:<18.6f} {:<14.6f} {:<16.2f}".format(
n,
"Prime" if res_normal else "Not Prime",
"Prime" if res_fft else "Not Prime",
normal_time,
fft_time,
improvement
))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python main.py <path_to_input_file>")
sys.exit(1)
input_file = sys.argv[1]
run_tests_from_file(input_file)