-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-prime-table.py
More file actions
95 lines (71 loc) · 2.56 KB
/
generate-prime-table.py
File metadata and controls
95 lines (71 loc) · 2.56 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
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
"""
Generates the `prime-table.scad` file used by the main
scad file
"""
### Settings
start_num=0 # The starting and ending numbers we will search for primes.
end_num=5000 # The first should generally be 0.
output_file="prime_table.scad"
column_width = 8 # Number of primes between newlines in the generated SCAD file
display_output = True # Display what we will be placing in the file? Useful for debugging
### Functions / etc
def is_prime(num):
"""Returns True if the number is prime
else False."""
if num == 0 or num == 1:
return False
for x in range(2, num):
if num % x == 0:
return False
else:
return True
### Initialization
integer_list = range(start_num,end_num)
prime_list=[]
scadfile_header = f"""
/*
*** CHANGES TO THIS FILE MAY BE OVERWRITTEN ***
(Static) prime number list. Generally, you will not want edit this file!
If you want to change the number of primes, edit and re-run the generator.
On the last run, this file was generated by the following:
{__file__}
*** CHANGES TO THIS FILE MAY BE OVERWRITTEN ***
*/
primes = [
"""
scadfile_footer = f"""
];
/*
*** CHANGES TO THIS FILE MAY BE OVERWRITTEN ***
See note at top of file.
*/
"""
### Start Script
# Iterate through all of the integers we generated
for an_integer in integer_list:
if is_prime(an_integer): # If this one prime? If so...
prime_list.append(an_integer) # then add it to the list
# Start generating our actual file.
file_contents = scadfile_header
# Generate a comma-seperated list of our numbers
#num_list_string = prime_list.split(",")
#
#num_list_string = ','.join(map(str,prime_list))
# The following section of code exists strictly to make the line lengths in
# our generated .scad file a bit less nuts. We will be making an "outer" list
# that will later be split by newlines, and the inner one will be primes
# seperated by commas.
split_prime_list = [] # the "outer" list - each element is a string of `column_width` primes, seperated by commas
#numbers_string = ""
for x in range(0,len(prime_list),column_width):
# To "outer" list, append the relevant section of the prime list, joined by commas
split_prime_list.append(','.join(
map(str,prime_list[x: x + column_width]))
)
# Finish up our string by joining it with commas & newlines
numbers_string=',\n'.join(split_prime_list)
file_contents=scadfile_header + numbers_string + scadfile_footer
if display_output:
print(file_contents)
with open(output_file,'w') as output_file_obj:
output_file_obj.write(file_contents)