Competition techniques combining divisibility, congruences and Diophantine tricks to solve number puzzles.
IntuitionHow Many Times Does 2 Divide 100!?
How many zeros does 100! (that is, 1×2×3×⋯×100) end in? Counting every factor of 10 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 5 paired with a factor of 2 (and 2s are far more abundant), so you just need to count how many times 5 divides 100!. Every multiple of 5 up to 100 contributes at least one factor of 5 (20 multiples), every multiple of 25 contributes an extra one (4 multiples), and every multiple of 125 would contribute yet another (there are none ≤100), giving 20+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=13: tracking orbits and p-adic valuations νp(an−bn) turns Olympiad divisibility problems into modular arithmetic.
Schoolp-adic Valuation and Legendre's Formula
Definition: p-adic Valuation
For a prime p and a nonzero integer n, the **p-adic valuation** vp(n) is the largest exponent k such that pk∣n, i.e. n=pvp(n)⋅m with p∤m. It extends to products via vp(ab)=vp(a)+vp(b), turning multiplication into addition, exactly like a logarithm restricted to one prime.
vp(n!)=i=1∑∞⌊pin⌋
This is Legendre's formula: it counts, for each power pi, how many multiples of pi lie in {1,…,n}, and summing over all i counts every factor of p hidden inside n! exactly once for each level it survives to. The sum is finite in practice since ⌊n/pi⌋=0 once pi>n. A useful reformulation is vp(n!)=p−1n−sp(n), where sp(n) is the sum of digits of n written in base p.
For a prime p and positive integer n, vp(n!)=∑i=1∞⌊pin⌋.
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!) as a sum over the factors.** By definition n!=1⋅2⋯n, so vp(n!)=∑k=1nvp(k), the sum of the p-adic valuation of every integer from 1 to n.
**Step 2: Rewrite each vp(k) as a count.** For each k, vp(k)=∑i=1∞[pi∣k] (using Iverson bracket notation, 1 if true, 0 if false), since k is divisible by pi for exactly vp(k) values of i (namely i=1,…,vp(k)).
Step 3: Swap the order of summation. Substituting, vp(n!)=∑k=1n∑i=1∞[pi∣k]=∑i=1∞∑k=1n[pi∣k], exchanging the (finite, so justified) double sum.
**Step 4: Count multiples of pi directly.** The inner sum ∑k=1n[pi∣k] counts how many integers from 1 to n are multiples of pi, which is exactly ⌊pin⌋ (the multiples are pi,2pi,…,⌊n/pi⌋⋅pi).
Step 5: Conclude. Substituting back, vp(n!)=∑i=1∞⌊pin⌋, which is finite since ⌊n/pi⌋=0 once pi>n, completing the proof.
Let p be an odd prime, and let a,b be integers with p∣a−b and p∤a, p∤b. Then for every positive integer n: vp(an−bn)=vp(a−b)+vp(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−bn or to prove such an expression is never (or always) divisible by some prime power.
Proof
**Step 1: Reduce to the case n=p via multiplicativity.** Write n=pvp(n)⋅m with p∤m. Repeated application of the n=p case (proved below) to am,bm in place of a,b shows vp(an−bn)=vp((am)pvp(n)−(bm)pvp(n))=vp(am−bm)+vp(n), so it suffices to prove vp(am−bm)=vp(a−b) whenever p∤m, and to prove the base step vp(ap−bp)=vp(a−b)+1.
Step 2: Prove the base step using the factorization. Factor ap−bp=(a−b)(ap−1+ap−2b+⋯+bp−1). We must show the second factor S=∑j=0p−1ap−1−jbj has vp(S)=1.
**Step 3: Show p∣S.** Since p∣a−b, we have a≡b(modp), so each term ap−1−jbj≡bp−1−jbj=bp−1(modp). Summing all p terms, S≡p⋅bp−1≡0(modp) (using p∤b), so p∣S.
**Step 4: Show p2∤S.** Write a=b+pt for integer t (possible since p∣a−b). Expand each term ap−1−jbj=(b+pt)p−1−jbj≡bp−1−jbj+(p−1−j)ptbp−2−jbj(modp2) (binomial expansion, dropping terms with p2 or higher). Summing over j=0,…,p−1: the leading terms sum to pbp−1 as before, and the correction terms sum to ptbp−2∑j=0p−1(p−1−j)=ptbp−2⋅2p(p−1), which is divisible by p2 (since p is odd, 2p−1 is an integer, so this correction is p2⋅(integer), hence ≡0(modp2)). So S≡pbp−1(modp2), and since p∤b, pbp−1 is divisible by p but not p2, giving vp(S)=1.
Step 5: Combine. From Steps 2–4, vp(ap−bp)=vp(a−b)+vp(S)=vp(a−b)+1. Combined with the reduction in Step 1 (and the fact vp(am−bm)=vp(a−b) when p∤m, provable the same way since then S≡mbm−1≡0(modp)), induction on vp(n) gives vp(an−bn)=vp(a−b)+vp(n) for all positive integers n.
AdvancedReal-World Applications and Worked Examples
p-adic valuation is not just a competition curiosity: in cryptography, computing v2 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), a primitive used in bit-manipulation tricks, hash table implementations, and the classic "lowest set bit" trick 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=z4, a method now central to modern proofs in Diophantine geometry.
Example: Trailing Zero Bits via v2
A hash table implementation needs to find the number of trailing zero bits of a positive integer n=1600 in binary, a step used to compute which bucket level n belongs to in a bitwise trie. Compute v2(1600).
Solution
Step 1: Factor out powers of 2 repeatedly: 1600=2⋅800=22⋅400=23⋅200=24⋅100=25⋅50=26⋅25.
Step 2: Since 25 is odd, no more factors of 2 can be extracted, so 1600=26⋅25 with 25 odd, giving v2(1600)=6.
Step 3: Check against the binary representation: 1600=110010000002, which indeed has exactly 6 trailing zero bits, confirming v2(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,b be positive integers such that ab+1 divides a2+b2. Show that ab+1a2+b2 is a perfect square (the famous IMO 1988 Problem 6, considered one of the hardest problems in olympiad history).
Solution
Step 1: Let k=ab+1a2+b2 and suppose for contradiction k is a positive integer that is not a perfect square. Among all pairs (a,b) of nonnegative integers with ab+1a2+b2=k, choose one with a+b minimal, and assume without loss of generality a≥b≥0.
Step 2: Fix b and k, and treat a2−kb⋅a+(b2−k)=0 (rearranging a2+b2=k(ab+1)) as a quadratic in a. It has root a, so by Vieta's formulas the other root is a′=kb−a=ab2−k.
Step 3: Show a′ is an integer (clear from a′=kb−a) and a′≥0: if a′<0 then a′2−kba′+(b2−k)≥a′2+k+(b2−k)>0 contradicts a′ being a root (since the quadratic equals 0 there and every term but possibly −kba′ is nonnegative when a′<0, making the whole expression strictly positive — a contradiction), so a′≥0.
Step 4: Show (a′,b) is a smaller solution, deriving a contradiction: since a′=ab2−k and b<a (as a≥b and a=b would force k=2, a perfect square, contradicting our assumption unless a=b=0 excluded by positivity — the case a=b is handled separately and gives k=2's negation directly), we get a′=ab2−k<ab2≤aa2=a using b<a, wait more directly: a′a=b2−k<b2≤a2 so a′<a (using a>0), meaning the new pair (a′,b) has a′+b<a+b, a strictly smaller sum, while still satisfying a′b+1a′2+b2=k (the quadratic relation is symmetric in the sense that replacing a by the other root preserves the value k) — contradicting the minimality of a+b.
Step 5: Conclude. The contradiction in Step 4 shows no such minimal counterexample can exist, so k 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!)?
By Lifting The Exponent, for prime p=7 with 7∣(12−5) and 7∤12, 7∤5, what is v7(127−57)?
In Vieta jumping on ab+1a2+b2=k, given a solution (a,b) with a≥b, the other root of the quadratic in a is a′=kb−a. What key property must be shown about a′ to derive a contradiction from minimality of a+b?
The abc conjecture, if proven, would generalize the divisibility intuition behind which lemma discussed in this topic?