Purpose: Given a root of a polynomial modulo , lift it to a root modulo — doubling the precision each step. The -adic analogue of Newton’s method.
Hensel’s Lemma
Let and suppose with (a simple root). Then there is a unique lift with , given by
This is exactly Newton’s iteration , with the inverse taken modulo .
The linear (slower but simpler) version lifts one power at a time:
Code
// lift a root of f mod p to a root mod p^k, quadratic version
long long henselLift(function<long long(long long,long long)> f,
function<long long(long long,long long)> df,
long long r, long long p, int k) {
long long mod = p;
while (mod < ipow(p, k)) {
long long nmod = mod * mod; // double the precision
long long inv = modinv(df(r, nmod), nmod); // f'(r) is a unit
r = ((r - f(r, nmod) % nmod * inv) % nmod + nmod) % nmod;
mod = nmod;
}
return r % ipow(p, k);
}Complexity
Each step doubles the precision, so reaching takes iterations. With naive arithmetic on -bit numbers the total is where is the multiplication cost — the same asymptotic as a single multiplication at full precision.
Why It Works
Write . Taylor-expand:
(all higher terms carry ). Since , dividing by gives
which has a unique solution for precisely because is invertible mod . ∎
Singular roots
If , the lemma does not apply. The root may lift in several ways, in one way, or not at all — you must branch over the candidate lifts and test each.
Worked example: square roots mod
To solve , note is never a unit mod 2, so plain Hensel fails. The fix is a specialised iteration or a direct bit-by-bit lift — a standard reminder that needs care.
Variants / Use Cases
- Modular square roots mod — solve mod with Tonelli-Shanks, then lift
- Solving for composite — factor , solve each prime power by lifting, recombine with CRT
- Polynomial factorisation over $\mathbb{Z}$ — factor mod a small prime, Hensel-lift to a large prime power, then recombine factors
- -adic numbers — Hensel’s lemma is the statement that is “complete enough” for Newton’s method
- Newton iteration for formal power series — the exact same lifting idea with in place of : series inverse, log, exp and sqrt are all Hensel lifts