man number-theory/linear-sieve

lesson 6 of 27 · Primes and factorization

The linear sieve

lib / Number theory for competitive programming 15 mindraft

checked against: proofs in the text, code tested with Python 3.15.2

code: toothywalrus/cp-math@lesson-06

Up to 10610^6, lesson 5’s sieve made 2,122,048 writes to cross out 921,501 composites: 1,200,547 of the writes hit a number already crossed out, the way 30 is crossed out by 2, 3 and 5. The linear sieve writes each composite exactly once, as i⋅pi \cdot p where pp is its smallest prime factor. It does O(n)O(n) work instead of O(nlog⁡log⁡n)O(n \log \log n), and it leaves behind a table of every number’s smallest prime factor. From that table the same pass can compute the number of divisors, Euler’s φ\varphi or the Möbius function of every number up to nn. Linear does not mean faster, though, and this lesson measures that too.

The code is in cp-math at tag lesson-06, in cpmath/linear_sieve.py. Its tests check linear_sieve against brute force for every nn up to 300; its list of primes up to 10610^6 against lesson 4’s primes_by_trial and lesson 5’s primes_up_to; its table at 3,000 random m≤106m \le 10^6 against lesson 4’s smallest_prime_factor; and they count the writes to every entry up to 5000. Each function of the second half is checked for every mm up to 2000, against a brute-force count (dd, σ\sigma, φ\varphi) or lesson 4’s factorize (μ\mu, ω\omega), and against the factorization at 2000 random m≤105m \le 10^5 (φ\varphi at 100 other random mm, by counting gcds). python3 -m cpmath.lesson06 prints every Python output on this page (--time adds the timings). The C++ output comes from demos/linear_sieve.cpp.

One write per composite

Write lp⁡(m)\operatorname{lp}(m) for the smallest prime factor of m≥2m \ge 2. Every composite mm splits as m=i⋅pm = i \cdot p with p=lp⁡(m)p = \operatorname{lp}(m) and i=m/pi = m / p, and no prime factor of ii is smaller than pp, so p≤lp⁡(i)p \le \operatorname{lp}(i). The linear sieve runs that split backwards: for each ii, it multiplies ii by every prime p≤lp⁡(i)p \le \operatorname{lp}(i) and records pp as the smallest prime factor of i⋅pi \cdot p.

def linear_sieve(n):
    """(lp, primes) for n >= 0: lp[m] is the smallest prime factor of m
    for 2 <= m <= n (lp[0] = lp[1] = 0), primes the primes up to n.

    Invariant: when the outer loop is at i, every lp[m] with m <= i is
    final. The inner loop writes lp[i * p] = p for the primes p up to
    lp[i], so each composite is written once, by i = m // lp[m]."""
    lp = [0] * (n + 1)
    primes = []
    for i in range(2, n + 1):
        if lp[i] == 0:
            lp[i] = i
            primes.append(i)
        for p in primes:
            if p > lp[i] or i * p > n:
                break
            lp[i * p] = p
    return lp, primes

An lp[i] still 0 when the loop reaches ii means nothing wrote it, so ii is prime, and its smallest prime factor is itself. The figure runs it for n=50n = 50. Each composite is labelled with the i⋅pi \cdot p that wrote it:

The linear sieve on 1 to 50, one frame per ii that writes something. The label i⋅pi \cdot p under a composite is the only write it gets. Trace from python3 -m cpmath.linear_sieve_trace 50 in cp-math.

Why each composite is written exactly once

Lemma. Let i≥2i \ge 2 and let qq be a prime with q≤lp⁡(i)q \le \operatorname{lp}(i). Then lp⁡(iq)=q\operatorname{lp}(iq) = q.

Proof. A prime dividing iqiq divides ii or qq (Euclid’s lemma for primes, lesson 4), so it is qq or a prime factor of ii, which is at least lp⁡(i)≥q\operatorname{lp}(i) \ge q. So no prime factor of iqiq is below qq, and qq is one. ■\blacksquare

Theorem. After linear_sieve(n), lp[m] =lp⁡(m)= \operatorname{lp}(m) for 2≤m≤n2 \le m \le n, and the inner loop writes each composite m≤nm \le n exactly once, at i=m/lp⁡(m)i = m/\operatorname{lp}(m) and p=lp⁡(m)p = \operatorname{lp}(m). Primes are never written there.

Proof. By induction on ii, show that when the outer loop reaches ii, lp[i] is 0 if ii is prime and lp⁡(i)\operatorname{lp}(i) if not.

Suppose that holds for every value up to ii. Then the first test appends only primes to the list and sets lp[i] to lp⁡(i)\operatorname{lp}(i) for a prime ii too. The inner loop writes lp[i * p] = p only for primes p≤lp⁡(i)p \le \operatorname{lp}(i), so by the lemma each write stores the right value, the number written is composite, and it determines the pair: p=lp⁡(ip)p = \operatorname{lp}(ip), i=ip/pi = ip/p. So no entry is written twice, and no prime is ever written.

Every composite m≤nm \le n is written. Let p=lp⁡(m)p = \operatorname{lp}(m) and i=m/pi = m/p. No prime factor of ii is below pp, so p≤lp⁡(i)≤ip \le \operatorname{lp}(i) \le i, and pp joined the list when the outer loop was at pp. At ii, the inner loop runs through the primes in increasing order; each q<pq < p passes both tests (q<p≤lp⁡(i)q < p \le \operatorname{lp}(i) and iq<m≤niq < m \le n), and so does pp, so lp[m] = p is written, at step i<mi < m.

That gives the induction step: a composite mm holds lp⁡(m)\operatorname{lp}(m) from step m/lp⁡(m)m/\operatorname{lp}(m) on, and a prime is still 0 when the loop reaches it. ■\blacksquare

So the inner loop writes exactly n−1−π(n)n - 1 - \pi(n) times, and each ii adds at most one more look at a prime, the one that stops it. That is fewer than 2n2n steps in all: O(n)O(n).

What linear time buys

Counted in a run, against lesson 5’s writes:

n = 10^6: composites 921501, linear sieve writes 921501, primes looked at 1921332, Eratosthenes writes 2122048
n = 10^7: composites 9335420, linear sieve writes 9335420, primes looked at 19334973, Eratosthenes writes 22850051

Counting every look, the linear sieve takes slightly fewer steps than Eratosthenes makes writes. In Python each look is a trip round the interpreter, while lesson 5’s bytearray sieve did its writes in C. One run on an Apple M2 Pro, Python 3.15.2, read to one significant figure:

sieve(10**7): 0.790 s
sieve_flags(10**7): 0.013 s
linear_sieve(10**7): 0.834 s

The linear sieve was about as fast as the plain list sieve and some 60 times slower than the bytearray one. It also stores a Python int per entry instead of a byte, and sys.getsizeof counts only the list’s pointers, not the int objects they point to:

sys.getsizeof(lp) for 10**7 = 80000064, len(primes) = 664579, sys.getsizeof(primes) = 5933720

So in Python, use the bytearray sieve when only the primes are needed, and the linear sieve when the smallest prime factors or a multiplicative function are needed too.

In C++ (Apple clang 21, -O2), lesson 5’s vector<char> sieve against the linear sieve with a vector<int> table, one run:

n = 10^7  eratosthenes (vector<char>): pi = 664579  0.02 s  10000001 bytes
n = 10^7  linear (vector<int> lp):    pi = 664579  0.03 s  40000004 bytes for lp (+ 8388604 allocated as primes grew)
n = 10^8  eratosthenes (vector<char>): pi = 5761455  0.31 s  100000001 bytes
n = 10^8  linear (vector<int> lp):    pi = 5761455  0.31 s  400000004 bytes for lp (+ 67108860 allocated as primes grew)

Reruns varied by up to 0.03 s, and neither sieve was ahead in all of them at 10810^8. The linear sieve’s time also includes recording the largest product i * p at each look (the next section). Even so, it matches what cp-algorithms reports: in practice about as fast as a typical sieve of Eratosthenes. The cost is memory, 4 bytes per entry against 1 (or an eighth of one, packed), so 10810^8 does not fit a 256 MB limit.

Does i * p overflow?

The demo uses int and computes i * p before comparing it with nn. The first look multiplies by 2, giving at most 2n2n. Any later pp follows a prime p′p' that passed, so ip′≤ni p' \le n, and by Bertrand’s postulate (there is a prime between kk and 2k2k for every k>1k > 1) the next prime is p<2p′p < 2p'. So ip<2nip < 2n: no product exceeds 2n2n, which fits in an int for n≤1073741823n \le 1073741823. The demo records the largest product, and its build with -fsanitize=undefined reported nothing:

n = 10^8  same primes: yes; largest i * p computed: 200000000 (2n = 200000000)

The order of the two tests does not change this: the loop reaches a prime only after the one before it passed both, so the bound covers every product it computes, the one that stops the loop included.

Multiplicative functions in the same pass

A function ff on the positive integers is multiplicative if f(1)=1f(1) = 1 and f(ab)=f(a)f(b)f(ab) = f(a) f(b) whenever gcd⁡(a,b)=1\gcd(a, b) = 1. By the fundamental theorem of arithmetic (lesson 4), such an ff is fixed by its values on prime powers: f(p1e1⋯pkek)=f(p1e1)⋯f(pkek)f(p_1^{e_1} \cdots p_k^{e_k}) = f(p_1^{e_1}) \cdots f(p_k^{e_k}).

Example: the number of divisors d(n)d(n) is multiplicative, and d(pe)=e+1d(p^e) = e + 1. Proof. By uniqueness of factorization, the divisors of pep^e are 1,p,…,pe1, p, \dots, p^e. For coprime aa and bb, no prime divides both, so a divisor cc of abab splits uniquely as c=cacbc = c_a c_b, where cac_a collects the prime powers of cc at primes of aa, and cbc_b the rest. Then ca∣ac_a \mid a and cb∣bc_b \mid b, and conversely every such product divides abab. So the divisors of abab are in one-to-one correspondence with pairs of divisors of aa and of bb. ■\blacksquare

The linear sieve knows, at each write m=i⋅pm = i \cdot p, whether pp is new to mm. If p<lp⁡(i)p < \operatorname{lp}(i), then p∤ip \nmid i, so gcd⁡(i,p)=1\gcd(i, p) = 1 and f(m)=f(i)f(p)f(m) = f(i) f(p). If p=lp⁡(i)p = \operatorname{lp}(i), it is one more factor of a prime ii already has, and the sieve needs the whole power. It keeps qmq_m, the largest power of lp⁡(m)\operatorname{lp}(m) dividing mm, in the array pk. Then m=qmrm = q_m r with rr coprime to qmq_m, and f(m)=f(qm) f(r)f(m) = f(q_m)\, f(r):

def multiplicative(n, f_pk):
    """f[0..n] for the multiplicative function f with f(p**e) = f_pk(p, e)
    (f[0] = 0, f[1] = 1 for n >= 1).

    pk[m] is the largest power of lp(m) that divides m, so m = pk[m] * r
    with r = m // pk[m] coprime to it, and f(m) = f(pk[m]) * f(r)."""
    f = [0] * (n + 1)
    lp = [0] * (n + 1)
    pk = [0] * (n + 1)
    e = [0] * (n + 1)
    primes = []
    if n >= 1:
        f[1] = 1
    for i in range(2, n + 1):
        if lp[i] == 0:
            lp[i] = pk[i] = i
            e[i] = 1
            f[i] = f_pk(i, 1)
            primes.append(i)
        for p in primes:
            m = i * p
            if p > lp[i] or m > n:
                break
            lp[m] = p
            if p < lp[i]:                 # p does not divide i
                pk[m], e[m] = p, 1
                f[m] = f[i] * f[p]
            else:                         # p = lp(i): one more factor p
                pk[m], e[m] = pk[i] * p, e[i] + 1
                f[m] = f[i // pk[i]] * f_pk(p, e[m])
    return f

The value f[i // pk[i]] it reads is final. Each f[m] is written in the same step as lp[m]: for a composite mm, at the outer loop’s step m/lp⁡(m)<mm/\operatorname{lp}(m) < m (the theorem), for a prime at step mm, and f[1] before the loop. So when the loop reaches ii, every f[m] with m<im < i is final, and i/qi<ii / q_i < i. With f_pk = lambda p, e: e + 1 this is divisor_counts, and with (pe+1−1)/(p−1)(p^{e+1} - 1)/(p - 1), the sum of 1,p,…,pe1, p, \dots, p^e, it is divisor_sums; the sum of divisors is multiplicative by the same correspondence of divisors.

For many functions the power is not needed, because the rule for p=lp⁡(i)p = \operatorname{lp}(i) only looks at f(i)f(i). The Möbius function μ(n)\mu(n) is 0 when a square above 1 divides nn, and (−1)k(-1)^k when nn is a product of kk distinct primes. If p=lp⁡(i)p = \operatorname{lp}(i), then p2∣ipp^2 \mid ip, so μ(ip)=0\mu(ip) = 0. Otherwise ipip has one more prime than ii, and is squarefree exactly when ii is, so μ(ip)=−μ(i)\mu(ip) = -\mu(i). In the repo’s mobius_sieve that is one line, mu[i * p] = 0 if p == lp[i] else -mu[i].

Euler’s φ(n)\varphi(n) counts the kk from 1 to nn with gcd⁡(k,n)=1\gcd(k, n) = 1. Among 1,…,pe1, \dots, p^e, exactly the pe−1p^{e-1} multiples of pp share a factor with pep^e, so φ(pe)=pe−pe−1\varphi(p^e) = p^e - p^{e-1}. That φ\varphi is multiplicative is proved in the lesson on the totient; here it is assumed, and the tests check the result against a gcd count. From it: if p∤ip \nmid i, φ(ip)=φ(i)(p−1)\varphi(ip) = \varphi(i)(p - 1); if p=lp⁡(i)p = \operatorname{lp}(i) and pap^a is the power of pp in ii, the factor pa−pa−1p^a - p^{a-1} becomes pa+1−pap^{a+1} - p^a, which is pp times as large, so φ(ip)=p φ(i)\varphi(ip) = p\,\varphi(i):

def phi_sieve(n):
    """phi(m), the count of 1 <= k <= m with gcd(k, m) = 1, for
    0 <= m <= n (phi[0] = 0)."""
    phi = [0] * (n + 1)
    lp = [0] * (n + 1)
    primes = []
    if n >= 1:
        phi[1] = 1
    for i in range(2, n + 1):
        if lp[i] == 0:
            lp[i] = i
            phi[i] = i - 1
            primes.append(i)
        for p in primes:
            if p > lp[i] or i * p > n:
                break
            lp[i * p] = p
            phi[i * p] = phi[i] * p if p == lp[i] else phi[i] * (p - 1)
    return phi
d(1..12) = [1, 2, 2, 3, 2, 4, 2, 4, 3, 4, 2, 6]
mu(1..12) = [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0]
phi(1..12) = [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4]
omega(1..12) = [0, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2]

The last line is ω(n)\omega(n), the number of distinct prime factors (the repo’s omega_sieve). It is not multiplicative but additive, ω(ab)=ω(a)+ω(b)\omega(ab) = \omega(a) + \omega(b) for coprime a,ba, b, and the same pass computes it (exercise 4). In the timing run above, phi_sieve(10**6) took 0.135 s and divisor_counts(10**6) 0.153 s.

Practice problems

Each needs an idea from this lesson. Links go to the problem pages; the one line is what to use, not the statement.

  • LeetCode 1390. Four Divisors: dd and σ\sigma for every value up to 10510^5 from one pass; add σ(x)\sigma(x) for each xx with d(x)=4d(x) = 4.
  • LeetCode 2818. Apply Operations to Maximize Score: the prime score is ω\omega, from omega_sieve up to 10510^5; the rest is a monotonic stack (ties go to the smaller index) and taking the largest values first.
  • Codeforces 1512G. Short Task: σ(n)\sigma(n) for every n≤107n \le 10^7 from the linear sieve, then the smallest nn for each value up to 10710^7, and −1-1 for a value no nn reaches; since σ(n)>n\sigma(n) > n for n>1n > 1, no larger nn can give a value in range, and c=1c = 1 gives n=1n = 1.
  • Codeforces 1499D. The Number of Pairs: for each divisor gg of xx, x/g+dx/g + d must be divisible by cc, and then the coprime ordered pairs with product k=(x/g+d)/ck = (x/g + d)/c number 2ω(k)2^{\omega(k)}; kk goes up to 2⋅1072 \cdot 10^7, so sieve ω\omega that far.
  • Codeforces 1717E. Madoka and The Best University: for each cc and each divisor gg of s=n−cs = n - c, the pairs with a+b=sa + b = s and gcd⁡(a,b)=g\gcd(a, b) = g number φ(s/g)\varphi(s/g) when s/g≥2s/g \ge 2, and none when s/g=1s/g = 1; φ\varphi up to nn from phi_sieve.

Try it yourself

  1. In linear_sieve(50), which ii and pp write 36, and which write 45? Which ii writes the most entries, and which are they?
  2. For n=100n = 100, how many primes does the inner loop look at in all, and how many of those looks write? Explain the difference.
  3. Given φ(45)=24\varphi(45) = 24, μ(45)=0\mu(45) = 0 and d(45)=6d(45) = 6, use the sieve’s rules to get φ\varphi, μ\mu and dd of 90=45⋅290 = 45 \cdot 2 and of 135=45⋅3135 = 45 \cdot 3.
  4. Prove the rule omega_sieve uses: for i≥2i \ge 2 and a prime p≤lp⁡(i)p \le \operatorname{lp}(i), ω(ip)=ω(i)\omega(ip) = \omega(i) if p=lp⁡(i)p = \operatorname{lp}(i) and ω(i)+1\omega(i) + 1 otherwise.
  5. A number is perfect when σ(n)=2n\sigma(n) = 2n. Which n≤104n \le 10^4 are perfect?

Answers

Each one is asserted in tests/test_lesson06.py, by brute force or by running the sieve’s loop.

Show the answers
  1. 36=18⋅236 = 18 \cdot 2 and 45=15⋅345 = 15 \cdot 3: pp is the smallest prime factor and i=m/pi = m/p. i=7i = 7 writes the most, four entries: 14, 21, 35 and 49, one for each prime up to lp⁡(7)=7\operatorname{lp}(7) = 7. No other ii writes more than three.
  2. 169 looks, 74 of which write: one per composite up to 100. Each of the other 95 stops an ii. There are 99 values of ii; the 4 primes ii with i2≤100i^2 \le 100 (2, 3, 5, 7) reach the end of the list of primes instead of stopping on a look.
  3. lp⁡(45)=3\operatorname{lp}(45) = 3. For 90, p=2<3p = 2 < 3: φ(90)=24⋅1=24\varphi(90) = 24 \cdot 1 = 24, μ(90)=−0=0\mu(90) = -0 = 0, d(90)=6⋅d(2)=12d(90) = 6 \cdot d(2) = 12. For 135, p=3=lp⁡(45)p = 3 = \operatorname{lp}(45): φ(135)=3⋅24=72\varphi(135) = 3 \cdot 24 = 72, μ(135)=0\mu(135) = 0; for dd, q45=9q_{45} = 9, so 135=27⋅5135 = 27 \cdot 5 and d(135)=d(27) d(5)=4⋅2=8d(135) = d(27)\,d(5) = 4 \cdot 2 = 8.
  4. The prime factors of ipip are those of ii together with pp (Euclid’s lemma for primes). If p=lp⁡(i)p = \operatorname{lp}(i), pp is already one of them; if p<lp⁡(i)p < \operatorname{lp}(i), pp does not divide ii, so it is a new one.
  5. 6, 28, 496 and 8128.

Sources

  • cp-algorithms, Linear Sieve: the algorithm with the lp and pr arrays, the proof that each composite is visited once through its representation lp⁡(m)⋅x\operatorname{lp}(m) \cdot x with lp⁡(m)≤lp⁡(x)\operatorname{lp}(m) \le \operatorname{lp}(x), and the remark that in practice it runs about as fast as a typical sieve of Eratosthenes while needing an array of nn numbers instead of nn bits. It names the algorithm a variant of Algorithm 3.3 in P. Pritchard, “Linear prime-number sieves: a family tree”, Science of Computer Programming 9 (1987), 17–35.
  • Wikipedia, Sieve of Eratosthenes, section “Euler’s sieve”: the version in which each composite is eliminated exactly once, and its linear-time rediscovery by D. Gries and J. Misra, “A linear sieve algorithm for finding prime numbers”, Communications of the ACM 21(12) (1978), 999–1003.
  • Wikipedia, Multiplicative function: the definition, and the divisor function, φ\varphi and μ\mu as examples.
  • Wikipedia, Bertrand’s postulate: for every n>1n > 1 there is a prime pp with n<p<2nn < p < 2n, proved by Chebyshev in 1852.

ls number-theory/

Divisibility and the Euclidean algorithm

  • 01 GCD, Euclid, and Bézout draftWhy gcd(a, b) = gcd(b, a mod b), why it takes so few steps, extended Euclid and ax + by = c. Unlocks gcd-of-array, water jug and lcm problems.
  • 02 Integer division, floors and ceilings draft// and % on negative numbers in Python and C++, ceiling division without floats, math.isqrt. Unlocks problems whose answer is a count of multiples in a range.
  • 03 GCD over arrays and subarrays draftPrefix and suffix gcds, the gcd of differences, why a subarray's gcd changes only a logarithmic number of times. Unlocks subarray-gcd counting and remove-one-element problems.

Primes and factorization

  • 04 Primality and factoring by trial division draftWhy checking up to the square root suffices, and how far that goes in a time limit. Unlocks single-number primality and factorization queries.
  • 05 The sieve of Eratosthenes draftCrossing out multiples from p squared, its n log log n cost, and a fast bytearray version in Python. Unlocks prime counting and prime listing up to about ten million.
  • 06 The linear sieve draftEach composite crossed out exactly once, by its smallest prime factor, with multiplicative functions computed in the same pass.
  • 07 Smallest-prime-factor sieve draftStore each number's smallest prime factor and factor any number below the limit in logarithmic time. Unlocks problems that factor many numbers.
  • 08 The segmented sieve plannedPrimes in a window [L, R] with R far beyond what fits in memory, using only the primes up to the square root of R.
  • 09 Number and sum of divisors plannedBoth from the prime factorization, listing all divisors, and the harmonic-series sieve that does it for every n at once. Unlocks divisor-counting and perfect-number problems.
  • 10 Prime powers in factorials plannedLegendre's formula for the exponent of p in n!. Unlocks trailing-zero and divisibility-of-factorial problems.

Modular arithmetic

  • 11 Arithmetic mod m plannedCongruences, the remainder of a negative number, which operations survive reduction, and why so many problems use 10^9 + 7.
  • 12 Fast exponentiation plannedSquare-and-multiply, Python's three-argument pow, and matrix powers for linear recurrences. Unlocks huge-exponent and n-th Fibonacci problems.
  • 13 Modular inverses plannedDivision mod m by Fermat's little theorem and by extended Euclid, when an inverse exists, and all inverses of 1..n in linear time.
  • 14 Euler's totient and Euler's theorem plannedCounting the numbers coprime to n, by formula and by sieve, and reducing exponents mod phi(m). Unlocks power towers mod m.
  • 15 Linear congruences plannedSolving a x ≡ b (mod m), how many solutions there are, and when there are none.
  • 16 The Chinese remainder theorem plannedCombining congruences into one, for coprime and non-coprime moduli. Unlocks problems with several periodic conditions.
  • 17 Orders, primitive roots and discrete logarithms plannedThe order of an element, finding a generator, and baby-step giant-step for a^x ≡ b (mod m).

Counting mod p

  • 18 Factorials, inverse factorials and nCr plannedPrecompute factorials and their inverses once, then answer each binomial coefficient in constant time. Unlocks most counting problems with a prime modulus.
  • 19 Binomial identities and Pascal's triangle plannedPascal's rule for composite moduli, stars and bars, and Catalan numbers.
  • 20 Lucas's theorem plannednCr mod a small prime p when n is far larger than p, digit by digit in base p.
  • 21 Inclusion–exclusion plannedCounting by alternating sums over subsets: numbers coprime to n in a range, derangements, surjections.
  • 22 The Möbius function and Möbius inversion plannedInclusion–exclusion over divisors. Unlocks counting coprime pairs and sums of gcds.
  • 23 Divisor blocks: iterating over n // i plannedn // i takes only about 2 sqrt(n) distinct values; summing over blocks of equal value turns many O(n) sums into O(sqrt n).
  • 24 Sums over gcds plannedSums of gcd(i, n) and of gcd(i, j) over a grid, by grouping on the gcd and using the totient and Möbius function.

Large numbers

  • 25 The Miller–Rabin primality test plannedA probabilistic test, and a fixed set of bases that makes it exact for every 64-bit number. Unlocks primality checks up to 10^18.
  • 26 Pollard's rho factorization plannedFactoring 64-bit numbers by finding a cycle in a pseudo-random sequence, with Brent's improvement.
  • 27 Floor sums by a Euclid-like recursion plannedThe sum of floor((a i + b) / m) over a range in logarithmic time, by the same reduction Euclid's algorithm uses. Unlocks lattice-point counting under a line.