-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.py
More file actions
218 lines (197 loc) · 8 KB
/
pi.py
File metadata and controls
218 lines (197 loc) · 8 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
#[x-cmds]: UPDATE
"""Calculate the value of pi to a specified number of decimal places."""
from typing import Iterator
from xulbux.console import Throbber
from xulbux import FormatCodes, Console
import math
import time
import sys
ARGS = Console.get_args({"decimal_places": "before"})
REFERENCE_TIMES: dict[int, float] = {
1000: 0.01, # 1K DIGITS
5000: 0.175, # 5K DIGITS
10000: 0.75, # 10K DIGITS
25000: 5.10, # 25K DIGITS
50000: 25, # 50K DIGITS
100000: 120, # 100K DIGITS
500000: 3000, # 500K DIGITS
1000000: 75000, # 1M DIGITS
}
def get_hardware_score() -> float:
try:
import psutil
cpu_freq = psutil.cpu_freq()
max_freq = cpu_freq.max if cpu_freq else 3000
cpu_count = psutil.cpu_count(logical=False) or 1
memory = psutil.virtual_memory()
memory_factor = 1 + (0.3 * (1 - memory.available / memory.total))
return ((max_freq * math.sqrt(cpu_count)) / 4000) / memory_factor
except (ImportError, AttributeError):
# FALLBACK: USE DEFAULT HARDWARE SCORE (ASSUMES MODERN MID-RANGE SYSTEM)
return 1.0
def estimate_runtime(precision: int) -> float:
ref_points = sorted(REFERENCE_TIMES.keys())
if precision <= 100:
start_time = time.time()
_ = pi(precision)
return time.time() - start_time
if precision >= max(ref_points):
base_time = REFERENCE_TIMES[max(ref_points)]
scaling = (precision / max(ref_points))**2.0
if precision > 1000000:
scaling *= 1.2
else:
upper_idx = next(i for i, x in enumerate(ref_points) if x >= precision)
lower_idx = max(0, upper_idx - 1)
lower_point = ref_points[lower_idx]
upper_point = ref_points[upper_idx]
lower_time = REFERENCE_TIMES[lower_point]
upper_time = REFERENCE_TIMES[upper_point]
if lower_point == upper_point:
log_factor = 1
else:
raw_factor = precision / lower_point
log_factor = (math.log(raw_factor)**2) / (math.log(upper_point / lower_point))
base_time = lower_time * (upper_time / lower_time)**log_factor
scaling = 1.0
estimated_time = (base_time * scaling) / get_hardware_score()
if estimated_time < 0.01:
estimated_time = 0.01
if precision <= 5000:
correction_factor = 1.0
elif precision <= 10000:
correction_factor = 1.5
elif precision <= 25000:
correction_factor = 1.8
elif precision <= 50000:
correction_factor = 1.0
elif precision <= 100000:
correction_factor = 0.9
else:
correction_factor = 1.0
estimated_time *= correction_factor
return round(estimated_time, 2)
def format_time(seconds: float, short: bool = False, pretty_print: bool = False) -> str:
units = (
(
("SMBH", 1e106 * 365.25 * 24 * 60 * 60),
("HD", 1e100 * 365.25 * 24 * 60 * 60),
("BH", 1e40 * 365.25 * 24 * 60 * 60),
("DE", 1e14 * 365.25 * 24 * 60 * 60),
("SE", 1e12 * 365.25 * 24 * 60 * 60),
("GY", 225e6 * 365.25 * 24 * 60 * 60),
("HT", 13.8e9 * 365.25 * 24 * 60 * 60),
("Q", 1e30 * 365.25 * 24 * 60 * 60),
("R", 1e27 * 365.25 * 24 * 60 * 60),
("Y", 1e24 * 365.25 * 24 * 60 * 60),
("Z", 1e21 * 365.25 * 24 * 60 * 60),
("E", 1e18 * 365.25 * 24 * 60 * 60),
("P", 1e15 * 365.25 * 24 * 60 * 60),
("T", 1e12 * 365.25 * 24 * 60 * 60),
("G", 1e9 * 365.25 * 24 * 60 * 60),
("M", 1e6 * 365.25 * 24 * 60 * 60),
("k", 1e3 * 365.25 * 24 * 60 * 60),
("y", 365.25 * 24 * 60 * 60),
("mo", 30 * 24 * 60 * 60),
("w", 7 * 24 * 60 * 60),
("d", 24 * 60 * 60),
("h", 60 * 60),
("m", 60),
("s", 1),
),
(
("supermassive black hole lifespan", 1e106 * 365.25 * 24 * 60 * 60),
("universe heat death", 1e100 * 365.25 * 24 * 60 * 60),
("black hole era", 1e40 * 365.25 * 24 * 60 * 60),
("degenerate era", 1e14 * 365.25 * 24 * 60 * 60),
("stelliferous era", 1e12 * 365.25 * 24 * 60 * 60),
("galactic year", 225e6 * 365.25 * 24 * 60 * 60),
("Hubble time", 13.8e9 * 365.25 * 24 * 60 * 60),
("quetta-year", 1e30 * 365.25 * 24 * 60 * 60),
("ronna-year", 1e27 * 365.25 * 24 * 60 * 60),
("yotta-year", 1e24 * 365.25 * 24 * 60 * 60),
("zetta-year", 1e21 * 365.25 * 24 * 60 * 60),
("exa-year", 1e18 * 365.25 * 24 * 60 * 60),
("peta-year", 1e15 * 365.25 * 24 * 60 * 60),
("tera-year", 1e12 * 365.25 * 24 * 60 * 60),
("giga-year", 1e9 * 365.25 * 24 * 60 * 60),
("mega-year", 1e6 * 365.25 * 24 * 60 * 60),
("kilo-year", 1e3 * 365.25 * 24 * 60 * 60),
("year", 365.25 * 24 * 60 * 60),
("month", 30 * 24 * 60 * 60),
("week", 7 * 24 * 60 * 60),
("day", 24 * 60 * 60),
("hour", 60 * 60),
("minute", 60),
("second", 1),
),
)
parts: list[str] = []
b_val, val_name, a_name = (
"[b|br:cyan]" if pretty_print else "",
f"{'' if short else ' '}{'[_b|i|cyan]' if pretty_print else ''}",
"[_i|br:cyan]" if pretty_print else "",
)
for name, formula in units[0 if short else 1]:
if (val := int(seconds // formula)) > 0:
if not short:
val = f"{val:,}"
parts.append(f"{b_val}{val}{val_name}{name if val == '1' or short else f'{name}s'}{a_name}")
seconds %= formula
if not parts:
formatted_seconds = f"{f'{seconds:.3f}'.rstrip('0').rstrip('.')}"
parts.append(
f"{b_val}{formatted_seconds}{val_name}{units[0 if short else 1][-1][0] if seconds == 1 or short else f'{units[0 if short else 1][-1][0]}s'}{a_name}"
)
if short:
return ("[dim](:)" if pretty_print else ":").join(parts)
if len(parts) > 1:
return (("[dim] + [_dim]" if pretty_print else ", ").join(parts[:-1]) +
("[dim] + [_dim]" if pretty_print else " and ") + parts[-1])
return parts[0]
def p() -> Iterator[int]:
q, r, t, j = 1, 180, 60, 2
while True:
u, y = 3 * (3 * j + 1) * (3 * j + 2), (q * (27 * j - 12) + 5 * r) // (5 * t)
yield y
q, r, t, j = (10 * q * j * (2 * j - 1), 10 * u * (q * (5 * j - 2) + r - y * t), t * u, j + 1)
def pi(decimals: int = 10) -> str:
_p = p()
return "3." + "".join(str(next(_p)) for _ in range(decimals + 1))[1:]
def main() -> None:
global CALC_DONE
input_k = (
int(ARGS.decimal_places.values[0]) \
if ARGS.decimal_places.values and ARGS.decimal_places.values[0].replace("_", "").isdigit()
else 10
)
if (estimated_secs := estimate_runtime(input_k)) >= 604800:
FormatCodes.print(
f"\n[b|bg:black]( π [in]( CALCULATION WOULD TAKE TOO LONG ))\n\n{format_time(estimated_secs, pretty_print=True)}[_]\n"
)
else:
FormatCodes.print(
f"\n[dim](Will take about [b]{format_time(estimated_secs)}[_|dim] to calculate:)" if estimated_secs > 1 else ""
)
result = None
try:
with Throbber().context():
result = pi(input_k)
except MemoryError:
FormatCodes.print(
"\r[b|br:yellow](Your computer doesn't have enough memory for this calculation!)",
"[b|bg:black]( π [in]( CALCULATION WOULD TAKE THIS LONG IF YOU HAD ENOUGH MEMORY ))\n",
f"{format_time(estimated_secs, pretty_print=True)}[_]",
end="\n\n",
sep="\n",
)
except KeyboardInterrupt:
FormatCodes.print("\r[b|br:red](✗) \n")
sys.exit(0)
if result:
FormatCodes.print(f"\r[br:cyan]({result})\n")
else:
FormatCodes.print("\r[b|br:red](✗) \n")
if __name__ == "__main__":
main()