-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_cycle_finder.py
111 lines (85 loc) · 2.77 KB
/
run_cycle_finder.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
This script runs a program that tries to find cycles in Collatz
sequences and writes the results to disk.
Examples
--------
>>> python run_cycle_finder.py --k 201 --c 15 --v 1001 --f "data/cycles_c_15.csv"
"""
# Imports
import logging
import argparse
import shutil
from collatz.cycles import find_cycles
# Global variables
DATA_PATH = "data/"
DEFAULT_FILE_PATH = DATA_PATH + "cycles.csv"
DEFAULT_MAX_K = 999
DEFAULT_MAX_C = 1
DEFAULT_MAX_VALUE = 10000
DEFAULT_MAX_ITERATIONS = 100
def _parse_cmd_args():
"""
This function parses the command line arguments of the program.
:return: The parsed command line arguments.
"""
parser = argparse.ArgumentParser(description='Run cycle finder.')
parser.add_argument(
"--k", help=("maximum k factor. Default is " + str(DEFAULT_MAX_K)),
default=DEFAULT_MAX_K
)
parser.add_argument(
"--c", help=("maximum c summand. Default is " + str(DEFAULT_MAX_C)),
default=DEFAULT_MAX_C
)
parser.add_argument(
"--v", help=("maximum odd start value. Default is "
+ str(DEFAULT_MAX_VALUE)),
default=DEFAULT_MAX_VALUE
)
parser.add_argument(
"--f", help=("path of destination file. Default is '"
+ str(DEFAULT_FILE_PATH) + "'"),
default=DEFAULT_FILE_PATH
)
args = parser.parse_args()
return args
def _main():
"""
This function executes the program.
:return: None.
"""
# Configuration
logging.basicConfig(
level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
# Parsing command line args
args = _parse_cmd_args()
logging.debug("Command line args: %s", args)
k_factors = range(1, int(args.k) + 2, 2)
max_c = int(args.c)
max_value = int(args.v)
export_file_name = args.f
dest_file_name = export_file_name
tmp_file_name = export_file_name + "_tmp"
cycle_count = 0
write_mode = True
# Find cycles
logging.info("Running cycle finder...")
for k_factor in k_factors:
logging.info("Finding cycles for k=%d", k_factor)
cycle_frame = find_cycles(
k_factor, max_c=max_c, max_value=max_value,
max_iterations=DEFAULT_MAX_ITERATIONS)
cycle_count = cycle_count + len(cycle_frame)
# Write the frame to file
mode_flag = "w" if write_mode else "a"
cycle_frame.to_csv(
tmp_file_name, mode=mode_flag, index=False, header=write_mode)
write_mode = False
# Moving tmp file to destination file
logging.info("Moving temp file to destination file")
shutil.move(tmp_file_name, dest_file_name)
# Print results
logging.info("%d cycle(s) found:", cycle_count)
# Main block to start the program
if __name__ == '__main__':
_main()