← LibrarySpeeding Up Algorithms via Modular ComputationEngineering · MathematicsLesson 58/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Integer Algorithms

Speeding Up Algorithms via Modular Computation

The modular method for exact computation: bounding the result, computing modulo several primes, and reconstructing.

Page KV-MATH-0329Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Exact integer computations frequently produce intermediate values far larger than the final answer. Determinants of integer matrices are the standard example: entries stay small, intermediate minors do not.

The modular method bounds the answer in advance, computes modulo enough small primes to determine it, and reconstructs. Every intermediate stays within a machine word.

Learning objectives

  1. Recognise intermediate expression swell and its cost.
  2. Derive a bound on the result to fix the number of primes needed.
  3. Assemble the full modular pipeline.

01Intermediate expression swell

Gaussian elimination over the rationals on an integer matrix produces fractions whose numerators and denominators grow rapidly, even when the determinant itself is small. The growth is not an artefact of a poor implementation; it is inherent to the elimination order.

The modular method eliminates the swell by never leaving the range of a machine word.

02The pipeline

  1. Bound the answer

    Derive an a priori bound H on the absolute value of the result — for determinants, Hadamard's bound from the row norms.

  2. Choose primes

    Select distinct primes p₁, ..., p_k, each fitting a machine word, with product exceeding 2H.

  3. Compute modulo each

    Run the algorithm in Z_{pᵢ} for each i. All arithmetic is single-precision.

  4. Reconstruct

    Chinese remainder the residues to recover the value modulo the product.

  5. Recentre

    Map the result into the symmetric range (−H, H] to recover the signed answer.

Hadamard bound: |det A| ≤ ∏ᵢ ||rowᵢ||₂

03Unlucky primes and how to handle them

A prime is unlucky if the algorithm behaves differently modulo that prime than over the integers — for elimination, if a pivot that is non-zero over the integers vanishes modulo p.

Failure modes of the modular method
SymptomCauseRemedy
Rank drops mod pp divides a leading minorDiscard p, use another prime
Result inconsistent across primesOne or more unlucky primesMajority agreement, or add primes and recheck
Reconstruction unstable as primes are addedBound H was too smallRecompute the bound or add primes until stable

Unlucky primes are rare — the bad primes divide a fixed non-zero integer, so only finitely many exist — and random selection from a large pool makes the probability of hitting one negligible. Robust implementations add primes until the reconstructed value stops changing.

04Frequently asked questions

Why not use rational arithmetic with gcd reduction instead?

Because reducing fractions at every step requires a gcd computation per operation, which is more expensive than the arithmetic itself, and the numerators still grow between reductions. The modular method avoids fractions entirely.

How many primes are typically needed?

Enough that their product exceeds twice the bound. For word-size primes near 2^62 and a determinant bound of a few thousand bits, that is a few dozen primes — each computation being fast enough that the total remains far below the direct approach.

Does this apply beyond linear algebra?

Widely. Polynomial gcds, resultants, factorisation over the integers and Groebner basis computation all use modular techniques for the same reason: the answer is small and the intermediates are not.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 63-66.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

Modular Inverses and Chinese RemainderingArticle · MathematicsNEXT LESSON →Rational ReconstructionArticle · MathematicsThe Extended Euclidean AlgorithmArticle · MathematicsRational Reconstruction in Symbolic AlgebraArticle · Mathematics