I have four ranks beyond rank 4,000 and a 128-byte marking buffer. Where does sharing save the most?
I compare one traversal to the last request against four independent traversals using the same buffer. I check the values by direct sieve and external counts at each endpoint. For offsets 61, 62, 63 and 64 I mark 6 windows instead of 24. For 1, 8, 32 and 64 the largest request is the same and I go from 11 to 6. The 128 bytes count only the marking buffer.
I will compare batching queries arriving in a prescribed order. AT.EX21.03 · Try the experiment
Sharing a sieve segment
I request four prime ranks beyond the same anchor. How many marking operations can I save?
I mark up to the largest requested rank and read offsets 1, 8, 32 and 64. I compare separate queries and check the prime counts at each answer and its predecessor. Beyond rank 4,000 I count 3,982 shared marks versus 6,543 separate marks and 173 visits versus 285. The saving concerns sieve work from a known anchor.
I will measure the effect of rank dispersion at a fixed memory budget. AT.EX20.03 · Change the inputs.
Prefiltering an interval of candidates
Correct a candidate rank
At rank 100 the 30-wheel proposes 367. I recover 541 after 47 rejections among 147 candidates. Raising the wheel to 210 leaves 27 rejections. Interactive experiment · AT.EX18.03
I measure the work a small wheel can save before a primality test.
Filter contract
For a stream of integers I can discard multiples of 2, 3 and 5 before running a more expensive test. I handle those three primes separately. In factorial coordinates, Wilson’s theorem even gives the digit to remove.
End-to-end check
For every integer from 2 through 99, I check the survivors of the wheel of period 30 by trial division up to the square root. The first surviving composite is 49, followed by 77 and 91. I include these false positives as a test of the interface between screening and deciding.
Application to test
On a real input stream I would compare tests avoided and total time, including filter setup, against a segmented sieve. The benefit depends on the final test cost and reuse of the wheel.
Reproducing the example
After extracting the files, I run python experience.py (Python 3.10+). The file resultats.json contains the reference output.
Prior work
Counting actual primes between the first square and first cube
I fix consecutive primes p,q and an integer p≤X<q³. I write Cₚ(X+1) for the count of integers in [1,X] surviving wheels through p. A surviving composite has at least two prime factors, each ≥q. Three would give a product ≥q³. It is therefore uniquely a·b with primes q≤a≤b, including squares. Removing 1 and these products and restoring the sieved primes gives
π(X) = Cₚ(X+1) − 1 + π(p) − #{prime pairs (a,b) : q≤a≤b and ab≤X}.
This decomposition belongs to the classical Meissel–Lehmer method. I contribute a reproducible calibration of the wheel’s rank interface with explicit inputs, first failure and measured costs. At X=q³ the unchanged formula counts exactly one extra integer, q³. Lagarias, Miller and Odlyzko
I compare a sorted list of products with grouping the larger factors b by equal quotient ⌊X/b⌋. An independent Eratosthenes sieve checks every threshold. For p=5,7,11,19 I get 15,996 exact counts below the cube and all four predicted failures. At 343, for example, the counter returns 69 against 68 actual primes. I also check 28 preselected rank queries and 52 external calls to primecount 9.1, including 28 independent nth-prime queries. primecount
Cost of the inputs. At p=19 my batch uses 91 admissible primes through 529, 256 inclusion-exclusion terms and, for the first method, 634 stored products. On this machine the five-run medians are 0.429 ms to produce the factors and 0.115 ms to prepare the products. For the 12,148 thresholds from 19 to 12,166, sorted products and grouped quotients take 189.4 and 367.9 ms. The full sieve takes 1.10 ms to prepare and 1.84 ms for the list queries. I include the seven selections, separate Python allocation peaks and raw repetitions. These small domains favour an in-memory sieve. For a prospective law of prime occurrence I will ask which input information it actually saves.
I run python rang_premiers.py and use python rang_premiers.py --measure for timing and memory. The optional --primecount PATH uses a separately installed executable for external checks. Register
Initial state and quotient-counting cost
With python quotients.py I reproduce the square and cube tests, four powers of ten, two batches of 64 thresholds and selections at ranks 10, 100 and 1,000. Obtaining 29, 541 and 7,919 by binary search rebuilds 14, 13 and 14 quotient domains. With python quotients.py --primecount PATH I also compare 153 counts and three selections with primecount 9.1. I supply both outputs and the measurements. The sieve article explains the X=30 trace.
At X=12,166 the pair correction supplies eight wheel primes, builds 91 correction primes up to 528 and indexes 634 products. I count 170 query divisions for the pair variant and 256 for grouped correction. On the shared batch the quotient sieve takes 9.89 ms rebuilding for each threshold. The prepared corrections take 1.17 and 2.12 ms. At one million the quotient sieve takes 4.63 ms for one query including setup and 175.27 ms for 64 queries. Preparing the direct sieve table takes 115.72 ms and looking up the 64 answers takes 0.014 ms. I give medians of five Python runs on this machine. Including setup means rebuilding algorithm state on each call. The primecount CLI timings include a new process per query. Method · Ranks and references
I identify the exact scalar recurrence with the Lucy_Hedgehog prime-counting algorithm as explained and implemented by Griffin Macris. I use its basic quotient version in this comparison. Griffin Macris, Lucy’s Algorithm + Fenwick Trees
Replaying shared counts
I run python partager.py and python intervalle.py in standard Python. The first script fixes the batches, including a draw with seed 6109 and eight repeated requests, and checks every final key against an Eratosthenes sieve. It also checks duplication and order invariance. With --primecount PATH I compare all 183 distinct thresholds with primecount 9.1. The second script verifies every interval in [0,128] by explicit union and an independent count.
Running each script with --measure produces five timings and one Python allocation peak per operation. In the first protocol, consecutive, spread and seeded batches take 20.05, 60.40 and 50.07 ms for shared counts including setup. Independent runs take 292.85, 212.47 and 193.20 ms. Deduplicating the independent seeded batch gives 175.78 ms. On the spread batch I measure a 919,028-byte shared peak versus a 216,548-byte sequential peak. In terms of integer keys the batch [2,3] already exhibits the memory cost. The second protocol compares three strategies in the same trial on the dense batch. Its medians are in mesures-intervalle.json and the article. Proof and references · Shared counts · Anchor and segment
Counting primes at multiple thresholds
I sort the distinct requests and charge a window [a,b] the cost A(a−1)+c(b−a+1). A accounts for computing π(a−1), after which I sieve the window. Consecutive thresholds x<y can share a window. Cutting between them saves a scan of y−x−1 integers and costs an anchor A(y−1). This difference is independent of the other cuts, so one pass over gaps minimises the model exactly. For a general cost K(i,j) on the ith through jth thresholds I can still minimise D(j)=minᵢ[D(i−1)+K(i,j)], with D(0)=0 and indices starting at 1. Every partition ends in one such interval. Sharing quotient tables across windows would introduce another dependency. Bellman on dynamic programming · Segmented sieve
I freeze the coefficients on three training batches and compare four strategies on fifteen batches of 64 requests. A single window has the best median in all twelve new clustered batches. On the older spread batch, the quotient union takes 52.69 ms, one window takes 60.35 ms and the nine windows selected by dynamic programming take 61.01 ms. On the batch with duplicates, the partition uses 622,288 traced bytes against 1,957,187 for one segment. In a follow-up run the linear decision recovers every partition in 27 to 44 µs on these fifteen batches. I supply individual timing samples and separately traced memory.
I enumerate all 128 partitions of eight requests and 2,048 partitions of twelve. At eight, the model selects four windows. The sum of individually measured window times favours three at 1.5363 ms versus 1.5747 ms. At twelve both choose the same six windows. The timings depend on the machine and Python kernel. I check the ranks and anchors against a direct sieve, shuffle requests with duplicates, and check 108 distinct values with primecount. Data and method · Failure and next test · Independent counter
I run python fenetres.py with the frozen model. --primecount PATH adds the external check. --measure runs the three-repeat benchmark and --linear the seven-repeat follow-up. --fit prints a new calibration. To reproduce published boundaries I keep modele-07.json. The training and evaluation seeds are 7106 and 7107. Memory is the peak of Python allocations. stage_ns gives base-prime preparation, anchor calculation, marking, scanning, sorting, decision and scatter times.
I retain the complete cost matrices of both small batches. For calibration A(z)=α max(1,z)^0.75, I obtain α=154.3096 and c=69.2904 in nanosecond units. I assume fresh anchor calculations and a shared prime base. Wall time includes sorting and restoring request order. The direct sieve and primecount verify the answers.
Sieving by byte ranges
I mark composites in a window [a,b] and count bytes equal to one from the end of the preceding range through each next threshold. The ranges are disjoint and cover exactly b−a+1 bytes. I add π(a−1) and restore every answer to its input position, including duplicates. I check endpoints on 1,176 intervals, every result against a direct sieve and 213 values against primecount 9.1. Python byte counting · primecount
I keep fifteen older batches, calibrate on seven batches with seed 8201 and evaluate seventeen with seed 8202. I compare a single window, the additive model's best partition with at least two windows when feasible, a quotient union, and automatic selection. I include the cost of estimating quotient keys and deciding. On held-out batches one window has the best median sixteen times, the union once, and the model selects all seventeen winning backends. Decision overhead can erase a small gain. On the older spread batch one window now takes 4.475 ms versus 64.129 ms for the union, at 2,005,943 traced Python bytes versus 919,068. The earlier integer-by-integer Python scan had reversed the timing ranking. I provide five samples, lengths, scans on prepared marks and separately traced memory. Protocol and material
I then specified a targeted challenge without changing any coefficients. For 450,000 and 900,000 the first quotient domain is contained in the second. The model selects a window. Its measured median is 5.160 ms against 4.648 ms for the union. I retain this miss and the large segment's peak memory as inputs for the next criterion. First targeted miss
I run python plages.py with the frozen coefficients. --fit prints another calibration, --measure reruns the fifteen historical and seventeen held-out batches, --challenge reruns the subsequent challenge and --primecount PATH adds external verification.
Integer division guarantees nested quotient sets
For x=floor(y/m) I have floor(x/j)=floor(y/(mj)), so the quotient domain of x is contained in that of y. I check 2,739 parameter pairs. With y=900,000, shifting 450,000 down by one leaves 42 keys outside the larger domain, shifting it up leaves one. This asymmetry matters when estimating union cost.
I timed the seven offsets using the frozen previous model, five repeats per method. Union wins by median at 0, +1 and +2. One window wins at −64, −2, −1 and +64. The selector always chooses one window. At −1 I measure 4.303 ms for the window and 4.388 ms for union. At +1 I get 4.338 and 4.152 ms. These small local differences warrant isolated-process timing. python inclusion09.py --measure reproduces timings and allocation peaks.
My next test is to time both sides of the offset under the same memory cap. AT.P10 · Walisch, primecount
I run python inclusion09.py.
A state collision for primality
The integers 19 and 49 share a state modulo 30. One is prime and the other is 7 squared. Adding wheel 7 eliminates 49, then 121 is the first surviving composite above 7. I verify survivors up to 1,000 by trial division. No rule depending only on this state can separate 19 and 49.
The collision generalizes. For any fixed modulus M≥2 and prime q not dividing M, q and q(1+M) have the same residue. The latter is composite. Choosing q above any specified threshold gives a collision beyond that threshold. This elementary argument identifies what a proposed new algebra must add.
My next test is to specify another state variable and commit to an out-of-sample prediction. AT.P11 · Walisch, primesieve
I run python inclusion09.py.
A ranking reversal across three new batches
I freeze the earlier selector for three threshold pairs around 405,000 and 810,000. It chooses the single window three times. With five fresh processes per method and batch, observed median winners are single, single and union. Under a 300,000-byte Python allocation budget, union and separate windows fit on all three. I check all four distinct π queries against a sieve and primecount 9.1.
I plan to predict memory feasibility first and declare an uncertainty region when timings overlap. AT.P12 · Walisch, primecount
I run python seuils10.py.
An enriched state that still loses addition
I augment the residue modulo 30 with divisibility flags for 7 and 11. On [150,400] the first surviving composite is the predicted 169 = 13². Prime 151 has the same state as 361 = 19². States of 2 and 32 agree, while those of 3 and 33 differ. I therefore cannot define a successor on this state alone. The full residue modulo 2,310 preserves addition but is periodic.
I plan to specify an operation carrying the state without the original integer and search a fresh interval for its first counterexample. AT.P13 · Walisch, primesieve
I run python seuils10.py.
One successor bit and a counting budget
The flag “11 divides x+1” separates 2 from 32. States for 402 and 552 agree while their successors differ. The sieve admits 403=13×31 on [401,700]. For three new batches with a 300,000-byte Python allocation budget, union has the fastest feasible median on this machine. I verify π with primecount.
Test an operation with evolving information and account for its update. AT.P14 · Walisch, primesieve
How many states advance the wheel?
I ask an autonomous deterministic machine to emit the cyclic sieve survival bit at each step for period 2,310. All 2,310 shifts have distinct future sequences, and windows of length 66 separate all shifts. In [701,1100], 701 and 881 share the enriched summary but have different successors, and composite 703 survives. I check π(700)=125 and π(1100)=184 with primecount.
I will identify and charge the additional information required by a proposed primitive. AT.P15 · Walisch, primesieve
For the state lower bound I take W(x)=1 when x is coprime to a square-free product R. For any shift d nonzero modulo R, some prime divisor p of R does not divide d. The Chinese remainder theorem gives x congruent to −d modulo p and to 1 modulo every other factor. Then W(x)=1 and W(x+d)=0. The R rotations of the periodic word therefore have distinct futures. For R=2,310 the shortest window separating all rotations has length 66. As for primality, any summary retaining only the residue modulo m assigns the same state to a prime p and the composite p(1+m), for m≥1. A new rule must describe the additional information it updates.
How many outputs identify a wheel position?
I observe the unit indicator under translation by one and compare every shift. The first horizons separating all positions are 14, 38 and 66 for periods 30, 210 and 2,310. I supply indistinguishable shifts at horizons 1, 2, 4 and 8. The primitive outputs wheel admissibility. Composite 703 remains a witness separating it from primality.
Test any new primitive specifying state, update and output on a reserved interval. AT.P16 · Berstel et al., Minimization of automata
Share corrections across adjacent prime ranks
I start from a certified anchor pₖ, mark multiples in the next segment, scan candidates coprime to 210 and retain each rejection’s least factor. A witness sieves independently from 2. I count visits and report segment preparation separately. For the 32 ranks following rank 4000 I visit 88 candidates. Restarting at the same anchor for each query takes 1,473 visits. This measures the scan after segment preparation.
Change the inputs · AT.EX19.03 · Walisch · primesieve
I reproduce · python rang19.py
Download bilingual starter kit ↓