Maths WorkshopMaths problems with full solutions
Exercise 1333Number theory › Divisibility and gcdOlympiadAdded 5 Nov 2026

v_p(n!) from the digits of n in base p

#Legendre's formula#digit sum#p-adic valuation#base p

Statement

Let pp be a prime and sp(n)s_p(n) the sum of the digits of nn in base pp.

  1. Prove that vp(n!)=n−sp(n)p−1v_p(n!)=\dfrac{n-s_p(n)}{p-1}.
  2. Deduce that 2n−1∣n!2^{n-1}\mid n! iff nn is a power of 22, and that v2(2nn)=s2(n)v_2\binom{2n}n=s_2(n).
Hint

Write n=∑aipin=\sum a_ip^i. What is ⌊n/pk⌋\lfloor n/p^k\rfloor in terms of the digits?

Show solutionHide solution

Solution

1. Write n=∑i≥0aipin=\sum_{i\ge0}a_ip^i with 0≤ai≤p−10\le a_i\le p-1. Then ⌊n/pk⌋=∑i≥kaipi−k\lfloor n/p^k\rfloor=\sum_{i\ge k}a_ip^{i-k}. By Legendre's formula,
vp(n!)=∑k≥1∑i≥kaipi−k=∑i≥1ai(1+p+⋯+pi−1)=∑i≥1aipi−1p−1.v_p(n!)=\sum_{k\ge1}\sum_{i\ge k}a_ip^{i-k}=\sum_{i\ge1}a_i\left(1+p+\dots+p^{i-1}\right)=\sum_{i\ge1}a_i\frac{p^i-1}{p-1}.
Since the term i=0i=0 contributes 00, the sum equals n−sp(n)p−1\dfrac{n-s_p(n)}{p-1}.

2. For p=2p=2: v2(n!)=n−s2(n)v_2(n!)=n-s_2(n). Since s2(n)≥1s_2(n)\ge1, v2(n!)≤n−1v_2(n!)\le n-1, with equality iff s2(n)=1s_2(n)=1, i.e. nn is a power of 22. So 2n−1∣n!2^{n-1}\mid n! iff nn is a power of 22.

Next, s2(2n)=s2(n)s_2(2n)=s_2(n), so
v2(2nn)=v2((2n)!)−2v2(n!)=(2n−s2(n))−2(n−s2(n))=s2(n).v_2\binom{2n}n=v_2((2n)!)-2v_2(n!)=(2n-s_2(n))-2(n-s_2(n))=s_2(n).

Check. (63)=20=22⋅5\binom63=20=2^2\cdot5 and s2(3)=2s_2(3)=2 ✓. (84)=70\binom{8}{4}=70, s2(4)=1s_2(4)=1, v2(70)=1v_2(70)=1 ✓.

Lesson. Legendre's formula is just a statement about the digits of nn in base pp; switching to digits often turns a messy floor sum into a clean closed form.