taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.2.5-12

Exercise 1.2.5-12

proved in Lean solution written

RatingM22
MarkersM (mathematical), recommended
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 1.2.5 of Volume 1. Companion chapter: Permutations and factorials

The task

Give the exponent μ\mu of a prime pp in n!n! as a simple formula in nn, pp and the digits aia_i of nn in base pp.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Start from eq. (8) and write each ⌊n/pj⌋\lfloor n/p^j \rfloor with the digits of nn.

Hint 2

Exercise 11 does the case p=2p = 2. The key sum is pi−1=(p−1)(pi−1+⋯+1)p^i - 1 = (p-1)(p^{i-1} + \cdots + 1).

Solution

Show the solution

Let n=∑i=0kaipin = \sum_{i=0}^{k} a_i p^i with 0≤ai<p0 \le a_i < p. For j≥1j \ge 1, the terms with i≥ji \ge j are multiples of pjp^j, and the terms with i<ji < j add up to at most (p−1)(pj−1+⋯+1)=pj−1<pj(p-1)(p^{j-1} + \cdots + 1) = p^j - 1 < p^j. So

⌊npj⌋=∑i≥jaipi−j.\left\lfloor \frac{n}{p^j} \right\rfloor = \sum_{i \ge j} a_i p^{i-j}.

By eq. (8),

μ=∑j≥1∑i≥jaipi−j=∑i=1kai(pi−1+⋯+p+1)=∑i=1kaipi−1p−1.\mu = \sum_{j \ge 1} \sum_{i \ge j} a_i p^{i-j} = \sum_{i=1}^{k} a_i \left(p^{i-1} + \cdots + p + 1\right) = \sum_{i=1}^{k} a_i \frac{p^i - 1}{p - 1}.

The sum over ii from 00 gives the same value, since the term i=0i = 0 is 00. So

μ=1p−1(∑i=0kaipi−∑i=0kai)=n−(a0+a1+⋯+ak)p−1.\mu = \frac{1}{p-1}\left(\sum_{i=0}^{k} a_i p^i - \sum_{i=0}^{k} a_i\right) = \frac{n - (a_0 + a_1 + \cdots + a_k)}{p - 1}.

Check

For n=100n = 100 and p=3p = 3 the base-3 digits are 1 0 2 0 11\,0\,2\,0\,1, with digit sum 44. The formula gives (100−4)/2=48(100 - 4)/2 = 48, and eq. (8) gives 33+11+3+1=4833 + 11 + 3 + 1 = 48. For n=20n = 20, p=5p = 5: 20=40520 = 40_5, so (20−4)/4=4(20 - 4)/4 = 4, as in exercise 6. Python confirms the formula for p∈{2,3,5,7}p \in \{2, 3, 5, 7\} and all n<300n < 300.

Remarks

Legendre's formula is also the number of carries seen when adding in base pp: the exponent of pp in (m+nn)\binom{m+n}{n} is the number of carries in the base pp sum m+nm + n, which follows from this formula applied to the three factorials. Exercise 1.2.5-14 refines it to a congruence for the part of n!n! that pp does not divide.

This solution is checked, and a person has not reviewed it yet. The source.

In Lean

proved The statement and its proof are in Lean. The kernel accepts the proof, and it uses the standard axioms only.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.