99IMPORTANT: Diagnostics must observe only. They must never alter computational results.
1010"""
1111
12- from typing import Any , Callable
13- from dataclasses import dataclass , field
12+ from collections .abc import Callable
13+ from dataclasses import dataclass
14+ from typing import Any
1415
1516
1617def verify_resolution (result : int , index : int , pi_func : Callable , is_prime_func : Callable ) -> bool :
@@ -35,9 +36,7 @@ def verify_resolution(result: int, index: int, pi_func: Callable, is_prime_func:
3536 """
3637 # Check primality
3738 if not is_prime_func (result ):
38- raise AssertionError (
39- f"Resolution verification failed: result { result } is not prime"
40- )
39+ raise AssertionError (f"Resolution verification failed: result { result } is not prime" )
4140
4241 # Check index
4342 pi_result = pi_func (result )
@@ -78,15 +77,15 @@ def verify_range(primes: list[int], is_prime_func: Callable) -> bool:
7877
7978 # Check strictly increasing
8079 for i in range (1 , len (primes )):
81- if primes [i ] <= primes [i - 1 ]:
82- raise AssertionError (
83- f"Range verification failed: not strictly increasing at index { i } "
84- )
80+ if primes [i ] <= primes [i - 1 ]:
81+ raise AssertionError (f"Range verification failed: not strictly increasing at index { i } " )
8582
8683 return True
8784
8885
89- def check_forecast_quality (index : int , forecast_value : int , pi_func : Callable , epsilon : float = 0.1 ) -> dict [str , Any ]:
86+ def check_forecast_quality (
87+ index : int , forecast_value : int , pi_func : Callable , epsilon : float = 0.1
88+ ) -> dict [str , Any ]:
9089 """
9190 Check forecast quality (Tier C sanity check).
9291
@@ -109,10 +108,10 @@ def check_forecast_quality(index: int, forecast_value: int, pi_func: Callable, e
109108 relative_error = abs (pi_forecast - index ) / index
110109
111110 return {
112- ' passed' : relative_error < epsilon ,
113- ' relative_error' : relative_error ,
114- ' pi_forecast' : pi_forecast ,
115- ' threshold' : epsilon ,
111+ " passed" : relative_error < epsilon ,
112+ " relative_error" : relative_error ,
113+ " pi_forecast" : pi_forecast ,
114+ " threshold" : epsilon ,
116115 }
117116
118117
@@ -132,10 +131,9 @@ def simulator_diagnostics(sequence: list[int], pi_func: Callable) -> dict[str, A
132131 Returns:
133132 Dictionary with diagnostic metrics
134133 """
135- import math
136134
137135 if not sequence :
138- return {' error' : ' empty sequence' }
136+ return {" error" : " empty sequence" }
139137
140138 n = len (sequence )
141139 q_final = sequence [- 1 ]
@@ -151,24 +149,26 @@ def simulator_diagnostics(sequence: list[int], pi_func: Callable) -> dict[str, A
151149 q_i = sequence [i ]
152150 pi_i = pi_func (q_i )
153151 ratio_i = pi_i / (i + 1 )
154- checkpoints .append ({
155- 'step' : i + 1 ,
156- 'q' : q_i ,
157- 'pi' : pi_i ,
158- 'density_ratio' : ratio_i ,
159- })
152+ checkpoints .append (
153+ {
154+ "step" : i + 1 ,
155+ "q" : q_i ,
156+ "pi" : pi_i ,
157+ "density_ratio" : ratio_i ,
158+ }
159+ )
160160
161161 # Compute drift (deviation from expected ratio of 1.0)
162162 drift = abs (density_ratio - 1.0 )
163163
164164 return {
165- ' n_steps' : n ,
166- ' q_final' : q_final ,
167- ' pi_final' : pi_final ,
168- ' density_ratio' : density_ratio ,
169- ' drift' : drift ,
170- ' convergence_acceptable' : drift < 0.15 , # Threshold from paper
171- ' checkpoints' : checkpoints ,
165+ " n_steps" : n ,
166+ " q_final" : q_final ,
167+ " pi_final" : pi_final ,
168+ " density_ratio" : density_ratio ,
169+ " drift" : drift ,
170+ " convergence_acceptable" : drift < 0.15 , # Threshold from paper
171+ " checkpoints" : checkpoints ,
172172 }
173173
174174
@@ -188,6 +188,7 @@ class ResolveStats:
188188 forecast_value: Initial forecast estimate
189189 final_result: Final resolved prime value
190190 """
191+
191192 pi_calls : int = 0
192193 binary_search_iterations : int = 0
193194 correction_backward_steps : int = 0
@@ -222,14 +223,15 @@ def set_result(self, value: int) -> None:
222223 def to_dict (self ) -> dict [str , Any ]:
223224 """Convert stats to dictionary for reporting."""
224225 return {
225- ' pi_calls' : self .pi_calls ,
226- ' binary_search_iterations' : self .binary_search_iterations ,
227- ' correction_backward_steps' : self .correction_backward_steps ,
228- ' correction_forward_steps' : self .correction_forward_steps ,
229- ' forecast_value' : self .forecast_value ,
230- ' final_result' : self .final_result ,
226+ " pi_calls" : self .pi_calls ,
227+ " binary_search_iterations" : self .binary_search_iterations ,
228+ " correction_backward_steps" : self .correction_backward_steps ,
229+ " correction_forward_steps" : self .correction_forward_steps ,
230+ " forecast_value" : self .forecast_value ,
231+ " final_result" : self .final_result ,
231232 }
232233
234+
233235@dataclass
234236class MeisselStats :
235237 """
@@ -245,6 +247,7 @@ class MeisselStats:
245247 recursion_depth_max: Maximum recursion depth reached
246248 recursion_guard_trips: Number of times recursion guard triggered
247249 """
250+
248251 phi_calls : int = 0
249252 phi_cache_size : int = 0
250253 pi_cache_size : int = 0
@@ -274,9 +277,9 @@ def increment_recursion_guard_trips(self) -> None:
274277 def to_dict (self ) -> dict [str , Any ]:
275278 """Convert stats to dictionary for reporting."""
276279 return {
277- ' phi_calls' : self .phi_calls ,
278- ' phi_cache_size' : self .phi_cache_size ,
279- ' pi_cache_size' : self .pi_cache_size ,
280- ' recursion_depth_max' : self .recursion_depth_max ,
281- ' recursion_guard_trips' : self .recursion_guard_trips ,
280+ " phi_calls" : self .phi_calls ,
281+ " phi_cache_size" : self .phi_cache_size ,
282+ " pi_cache_size" : self .pi_cache_size ,
283+ " recursion_depth_max" : self .recursion_depth_max ,
284+ " recursion_guard_trips" : self .recursion_guard_trips ,
282285 }
0 commit comments