MathLabs

Competition mathematics and problem solving

Olympiad number theory

Competition techniques combining divisibility, congruences and Diophantine tricks to solve number puzzles.

IntuitionHow Many Times Does 2 Divide 100!?

How many zeros does 100!100! (that is, 1×2×3×⋯×1001\times 2\times 3\times\cdots\times 100) end in? Counting every factor of 1010 hidden inside a product of a hundred numbers looks hopeless by brute force, yet there is a one-line trick: each trailing zero comes from a factor of 55 paired with a factor of 22 (and 22s are far more abundant), so you just need to count how many times 55 divides 100!100!. Every multiple of 55 up to 100100 contributes at least one factor of 55 (2020 multiples), every multiple of 2525 contributes an extra one (44 multiples), and every multiple of 125125 would contribute yet another (there are none ≤100\le 100), giving 20+4=2420+4=24 trailing zeros. This counting trick — finding the exact power of a prime dividing a factorial or a huge product — is the gateway into olympiad number theory: precise, mechanical rules that replace impossible-looking case counts with a few lines of arithmetic.

Parabola plot illustrating geometric decay of prime-power multiple counts
Multiplicative structure modulo m=13m = 13: tracking orbits and pp-adic valuations νp(an−bn)\nu_p(a^n - b^n) turns Olympiad divisibility problems into modular arithmetic.

Schoolpp-adic Valuation and Legendre's Formula

Definition: pp-adic Valuation

For a prime pp and a nonzero integer nn, the **pp-adic valuation** vp(n)v_p(n) is the largest exponent kk such that pk∣np^k \mid n, i.e. n=pvp(n)⋅mn = p^{v_p(n)} \cdot m with p∤mp \nmid m. It extends to products via vp(ab)=vp(a)+vp(b)v_p(ab) = v_p(a)+v_p(b), turning multiplication into addition, exactly like a logarithm restricted to one prime.

vp(n!)=∑i=1∞⌊npi⌋v_p(n!) = \sum_{i=1}^{\infty} \left\lfloor \frac{n}{p^i} \right\rfloor

This is Legendre's formula: it counts, for each power pip^i, how many multiples of pip^i lie in {1,…,n}\{1,\dots,n\}, and summing over all ii counts every factor of pp hidden inside n!n! exactly once for each level it survives to. The sum is finite in practice since ⌊n/pi⌋=0\lfloor n/p^i\rfloor = 0 once pi>np^i > n. A useful reformulation is vp(n!)=n−sp(n)p−1v_p(n!) = \frac{n - s_p(n)}{p-1}, where sp(n)s_p(n) is the sum of digits of nn written in base pp.

vp(n!)=n−sp(n)p−1v_p(n!) = \frac{n - s_p(n)}{p-1}
Which tool to reach for
ToolBest for
Legendre's formulaExact power of a prime dividing n!n! or binomial coefficients
Lifting The Exponentvp(an±bn)v_p(a^n \pm b^n) for p∣a∓bp \mid a\mp b
Vieta jumpingDiophantine equations symmetric under a quadratic substitution
Congruences mod nnRuling out solutions, periodicity arguments

UndergraduateFull Proofs: Legendre's Formula and Lifting The Exponent

For a prime pp and positive integer nn, vp(n!)=∑i=1∞⌊npi⌋v_p(n!) = \sum_{i=1}^{\infty} \left\lfloor \frac{n}{p^i} \right\rfloor.

Why is it true?

Legendre's formula is the standard tool for computing exact prime-power divisibility of factorials and binomial coefficients, and combined with Kummer's theorem it explains exactly which binomial coefficients are divisible by a given prime.

Proof

**Step 1: Write vp(n!)v_p(n!) as a sum over the factors.** By definition n!=1⋅2⋯nn! = 1\cdot 2\cdots n, so vp(n!)=∑k=1nvp(k)v_p(n!) = \sum_{k=1}^{n} v_p(k), the sum of the pp-adic valuation of every integer from 11 to nn.

**Step 2: Rewrite each vp(k)v_p(k) as a count.** For each kk, vp(k)=∑i=1∞[pi∣k]v_p(k) = \sum_{i=1}^{\infty} [p^i \mid k] (using Iverson bracket notation, 11 if true, 00 if false), since kk is divisible by pip^i for exactly vp(k)v_p(k) values of ii (namely i=1,…,vp(k)i=1,\dots,v_p(k)).

Step 3: Swap the order of summation. Substituting, vp(n!)=∑k=1n∑i=1∞[pi∣k]=∑i=1∞∑k=1n[pi∣k]v_p(n!) = \sum_{k=1}^n \sum_{i=1}^{\infty} [p^i \mid k] = \sum_{i=1}^{\infty} \sum_{k=1}^n [p^i \mid k], exchanging the (finite, so justified) double sum.

**Step 4: Count multiples of pip^i directly.** The inner sum ∑k=1n[pi∣k]\sum_{k=1}^n [p^i \mid k] counts how many integers from 11 to nn are multiples of pip^i, which is exactly ⌊npi⌋\left\lfloor \frac{n}{p^i} \right\rfloor (the multiples are pi,2pi,…,⌊n/pi⌋⋅pip^i, 2p^i, \dots, \lfloor n/p^i\rfloor \cdot p^i).

Step 5: Conclude. Substituting back, vp(n!)=∑i=1∞⌊npi⌋v_p(n!) = \sum_{i=1}^{\infty} \left\lfloor \frac{n}{p^i} \right\rfloor, which is finite since ⌊n/pi⌋=0\lfloor n/p^i \rfloor = 0 once pi>np^i > n, completing the proof.

Let pp be an odd prime, and let a,ba,b be integers with p∣a−bp \mid a-b and p∤ap \nmid a, p∤bp \nmid b. Then for every positive integer nn: vp(an−bn)=vp(a−b)+vp(n)v_p(a^n - b^n) = v_p(a-b) + v_p(n).

Why is it true?

LTE turns a hard question about divisibility of a difference of high powers into simple arithmetic on valuations, and is one of the fastest ways to solve olympiad problems asking for the largest power of a prime dividing an expression like an−bna^n-b^n or to prove such an expression is never (or always) divisible by some prime power.

Proof

**Step 1: Reduce to the case n=pn=p via multiplicativity.** Write n=pvp(n)⋅mn = p^{v_p(n)} \cdot m with p∤mp \nmid m. Repeated application of the n=pn=p case (proved below) to am,bma^m, b^m in place of a,ba,b shows vp(an−bn)=vp((am)pvp(n)−(bm)pvp(n))=vp(am−bm)+vp(n)v_p(a^n-b^n) = v_p((a^m)^{p^{v_p(n)}} - (b^m)^{p^{v_p(n)}}) = v_p(a^m-b^m) + v_p(n), so it suffices to prove vp(am−bm)=vp(a−b)v_p(a^m - b^m) = v_p(a-b) whenever p∤mp \nmid m, and to prove the base step vp(ap−bp)=vp(a−b)+1v_p(a^p-b^p) = v_p(a-b)+1.

Step 2: Prove the base step using the factorization. Factor ap−bp=(a−b)(ap−1+ap−2b+⋯+bp−1)a^p - b^p = (a-b)(a^{p-1}+a^{p-2}b+\cdots+b^{p-1}). We must show the second factor S=∑j=0p−1ap−1−jbjS = \sum_{j=0}^{p-1} a^{p-1-j}b^j has vp(S)=1v_p(S) = 1.

**Step 3: Show p∣Sp \mid S.** Since p∣a−bp \mid a-b, we have a≡b(modp)a \equiv b \pmod p, so each term ap−1−jbj≡bp−1−jbj=bp−1(modp)a^{p-1-j}b^j \equiv b^{p-1-j}b^j = b^{p-1} \pmod p. Summing all pp terms, S≡p⋅bp−1≡0(modp)S \equiv p\cdot b^{p-1} \equiv 0 \pmod p (using p∤bp \nmid b), so p∣Sp \mid S.

**Step 4: Show p2∤Sp^2 \nmid S.** Write a=b+pta = b + pt for integer tt (possible since p∣a−bp\mid a-b). Expand each term ap−1−jbj=(b+pt)p−1−jbj≡bp−1−jbj+(p−1−j)pt bp−2−jbj(modp2)a^{p-1-j}b^j = (b+pt)^{p-1-j}b^j \equiv b^{p-1-j}b^j + (p-1-j)pt\, b^{p-2-j}b^j \pmod{p^2} (binomial expansion, dropping terms with p2p^2 or higher). Summing over j=0,…,p−1j=0,\dots,p-1: the leading terms sum to p bp−1p\,b^{p-1} as before, and the correction terms sum to pt bp−2∑j=0p−1(p−1−j)=pt bp−2⋅p(p−1)2pt\,b^{p-2}\sum_{j=0}^{p-1}(p-1-j) = pt\,b^{p-2}\cdot\frac{p(p-1)}{2}, which is divisible by p2p^2 (since pp is odd, p−12\frac{p-1}{2} is an integer, so this correction is p2⋅(integer)p^2\cdot(\text{integer}), hence ≡0(modp2)\equiv 0 \pmod{p^2}). So S≡p bp−1(modp2)S \equiv p\,b^{p-1} \pmod{p^2}, and since p∤bp \nmid b, p bp−1p\,b^{p-1} is divisible by pp but not p2p^2, giving vp(S)=1v_p(S)=1.

Step 5: Combine. From Steps 2–4, vp(ap−bp)=vp(a−b)+vp(S)=vp(a−b)+1v_p(a^p-b^p) = v_p(a-b) + v_p(S) = v_p(a-b)+1. Combined with the reduction in Step 1 (and the fact vp(am−bm)=vp(a−b)v_p(a^m-b^m)=v_p(a-b) when p∤mp\nmid m, provable the same way since then S≡m bm−1≢0(modp)S \equiv m\,b^{m-1} \not\equiv 0 \pmod p), induction on vp(n)v_p(n) gives vp(an−bn)=vp(a−b)+vp(n)v_p(a^n-b^n) = v_p(a-b)+v_p(n) for all positive integers nn.

AdvancedReal-World Applications and Worked Examples

pp-adic valuation is not just a competition curiosity: in cryptography, computing v2v_2 of large numbers is a routine step in fast modular exponentiation and in analyzing the security margins of RSA-related constructions, while in computer science, counting trailing zero bits of a binary number is exactly v2(n)v_2(n), a primitive used in bit-manipulation tricks, hash table implementations, and the classic "lowest set bit" trick n  &  (−n)n \;\&\; (-n) used in Fenwick trees. The technique behind Vieta jumping — using a hidden quadratic symmetry to generate smaller solutions from larger ones — is a special case of the infinite descent method Fermat used to prove there are no nontrivial integer solutions to x4+y4=z4x^4+y^4=z^4, a method now central to modern proofs in Diophantine geometry.

Example: Trailing Zero Bits via v2v_2

A hash table implementation needs to find the number of trailing zero bits of a positive integer n=1600n=1600 in binary, a step used to compute which bucket level nn belongs to in a bitwise trie. Compute v2(1600)v_2(1600).

Solution

Step 1: Factor out powers of 22 repeatedly: 1600=2⋅800=22⋅400=23⋅200=24⋅100=25⋅50=26⋅251600 = 2\cdot 800 = 2^2\cdot 400 = 2^3\cdot 200 = 2^4\cdot 100 = 2^5\cdot 50 = 2^6\cdot 25.

Step 2: Since 2525 is odd, no more factors of 22 can be extracted, so 1600=26⋅251600 = 2^6\cdot 25 with 2525 odd, giving v2(1600)=6v_2(1600)=6.

Step 3: Check against the binary representation: 1600=1100100000021600 = 11001000000_2, which indeed has exactly 66 trailing zero bits, confirming v2(1600)=6v_2(1600)=6 matches the direct bit-counting approach and that the two methods (factoring and counting trailing bits) are the same operation.

Example: Vieta Jumping on IMO 1988 Problem 6

Let a,ba,b be positive integers such that ab+1ab+1 divides a2+b2a^2+b^2. Show that a2+b2ab+1\frac{a^2+b^2}{ab+1} is a perfect square (the famous IMO 1988 Problem 6, considered one of the hardest problems in olympiad history).

Solution

Step 1: Let k=a2+b2ab+1k=\frac{a^2+b^2}{ab+1} and suppose for contradiction kk is a positive integer that is not a perfect square. Among all pairs (a,b)(a,b) of nonnegative integers with a2+b2ab+1=k\frac{a^2+b^2}{ab+1}=k, choose one with a+ba+b minimal, and assume without loss of generality a≥b≥0a\ge b\ge 0.

Step 2: Fix bb and kk, and treat a2−kb⋅a+(b2−k)=0a^2 - kb\cdot a + (b^2-k) = 0 (rearranging a2+b2=k(ab+1)a^2+b^2=k(ab+1)) as a quadratic in aa. It has root aa, so by Vieta's formulas the other root is a′=kb−a=b2−kaa' = kb - a = \frac{b^2-k}{a}.

Step 3: Show a′a' is an integer (clear from a′=kb−aa'=kb-a) and a′≥0a' \ge 0: if a′<0a'<0 then a′2−kba′+(b2−k)≥a′2+k+(b2−k)>0a'^2 - kb a' + (b^2-k) \ge a'^2+k+(b^2-k) > 0 contradicts a′a' being a root (since the quadratic equals 00 there and every term but possibly −kba′-kba' is nonnegative when a′<0a'<0, making the whole expression strictly positive — a contradiction), so a′≥0a'\ge 0.

Step 4: Show (a′,b)(a',b) is a smaller solution, deriving a contradiction: since a′=b2−kaa'=\frac{b^2-k}{a} and b<ab<a (as a≥ba\ge b and a≠ba\ne b would force k=2k=2, a perfect square, contradicting our assumption unless a=b=0a=b=0 excluded by positivity — the case a=ba=b is handled separately and gives k=2k=2's negation directly), we get a′=b2−ka<b2a≤a2a=aa' = \frac{b^2-k}{a} < \frac{b^2}{a} \le \frac{a^2}{a} = a using b<ab<a, wait more directly: a′a=b2−k<b2≤a2a'a = b^2-k < b^2 \le a^2 so a′<aa'<a (using a>0a>0), meaning the new pair (a′,b)(a',b) has a′+b<a+ba'+b < a+b, a strictly smaller sum, while still satisfying a′2+b2a′b+1=k\frac{a'^2+b^2}{a'b+1}=k (the quadratic relation is symmetric in the sense that replacing aa by the other root preserves the value kk) — contradicting the minimality of a+ba+b.

Step 5: Conclude. The contradiction in Step 4 shows no such minimal counterexample can exist, so kk must in fact be a perfect square whenever it is a positive integer, proving the original claim.

Using Legendre's formula, what is v3(30!)v_3(30!)?

By Lifting The Exponent, for prime p=7p=7 with 7∣(12−5)7\mid (12-5) and 7∤127\nmid 12, 7∤57\nmid 5, what is v7(127−57)v_7(12^7-5^7)?

In Vieta jumping on a2+b2ab+1=k\frac{a^2+b^2}{ab+1}=k, given a solution (a,b)(a,b) with a≥ba\ge b, the other root of the quadratic in aa is a′=kb−aa'=kb-a. What key property must be shown about a′a' to derive a contradiction from minimality of a+ba+b?

The abc conjecture, if proven, would generalize the divisibility intuition behind which lemma discussed in this topic?

References

  1. Andrew Granville, Thomas J. Tucker (2002). It's As Easy As abc
  2. Titu Andreescu, Dorin Andrica, Zuming Feng (2007). 104 Number Theory Problems: From the Training of the USA IMO Team
  3. Titu Andreescu, Dorin Andrica, Ion Cucurezeanu (2010). An Introduction to Diophantine Equations: A Problem-Based Approach