Skip to content
Number Theory · Chapter 22

Divisibility & Legendre's Formula

Test divisibility from the digits alone, fill in missing digits, and count the factors of a prime inside a factorial without multiplying anything out.

You'll need
open on YouTube
22.0a0%

Divisibility Rules

In this section you will be able to test whether a number is divisible by 2–12 or 15 from its digits alone, and use the rules to find missing digits.

Find the smallest number made only of 4s and 9s that is divisible by 36. You never need to divide: the rule for 4 decides the ending, and the rule for 9 decides how many 4s you need.

Each rule looks at only part of the number. For 22, 44, 55, 88 and 1010 you check the last few digits, because 1010, 100100 and 10001000 are already multiples of those divisors. For 33 and 99 you check the digit sum, because 10≡110 \equiv 1 (mod 99).

For 1111, add the digits in the odd positions and the digits in the even positions separately. Look: 10≡−110 \equiv -1 (mod 1111), so the place values alternate between +1+1 and −1-1.

The rules for 66, 1212 and 1515 just combine two simpler rules. A missing digit becomes an unknown in the rule, and the rule turns into a small equation.

Exploreplay first, formulas later

444944 is not divisible by 9: Digit sum 4 + 4 + 4 + 9 + 4 + 4 = 29, not a multiple of 9.

Tap a digit to add 1 to it.

  • Digit sum 4 + 4 + 4 + 9 + 4 + 4 = 29, not a multiple of 9.
444944 is not divisible by 9

Check by dividing: 444944 ÷ 9 = 49438 remainder 2

444944 is not divisible by 9. Digit sum 4 + 4 + 4 + 9 + 4 + 4 = 29, not a multiple of 9.

Name it

Concept 22.0.1 · Divisibility Rules

Divisor Rule
22 Last digit is even
33 Sum of digits is divisible by 33
44 Last 22 digits divisible by 44
55 Last digit is 00 or 55
66 Divisible by 22 and 33
77 Take out multiples of 77 until you reach a small number that is either divisible or not divisible by 77
88 Last 33 digits are divisible by 88
99 Sum of digits is divisible by 99
1010 Last digit is 00
1111 Add the digits in the odd positions (OO) and the digits in the even positions (EE). If ∣O−E∣\lvert O - E \rvert is divisible by 1111, then the number is also divisible by 1111
1212 Divisible by 33 and 44
1515 Divisible by 33 and 55

Watch itpredict, then reveal

Example 22.1

AMC 10

Let nn denote the smallest positive integer that is divisible by both 44 and 99, and whose base-10 representation consists of only 44's and 99's, with at least one of each. What are the last four digits of nn?

Divisibility lens (its total is revealed at the end of the example)

  • Last 2 digits: 49 = 4 × 12 + 1, not a multiple of 4.
? (hidden until the end of the example)

Check by dividing: 49 ÷ 4 = ? (hidden until the end of the example)

49 ÷ 4. Last 2 digits: 49 = 4 × 12 + 1, not a multiple of 4.

  1. 1

    Start with 44. Only the last two digits matter. Which ending made of 44s and 99s is a multiple of 44?

    Predict first:
    Click a choice or type its letter (A–E).
open on YouTube

Try it

Problem 22.1.2

AMC 12

This problem also appears in the book as 21.3.15.

How many odd positive 33-digit integers are divisible by 33 but do not contain the digit 33?

Problem 22.1.6

AMC 10

Call a positive integer an uphill integer if every digit is strictly greater than the previous digit. For example, 13571357, 8989, 55 are all uphill integers but 3232, 12401240, 466466 are not. How many uphill integers are divisible by 1515?

Contest cornerreal contest problems

Problem 22.1.5

AIME

What is the largest positive integer nn for which n3+100n^3 + 100 is divisible by n+10n + 10?

Problem 22.1.7(additional)

AMC 10

What is the largest integer that is a divisor of

(n+1)(n+3)(n+5)(n+7)(n+9)(n + 1)(n + 3)(n + 5)(n + 7)(n + 9)

for all positive even integers nn?

Problem 22.1.13(additional)

AMC 10

For how many positive integers n≤1000n \le 1000 is

⌊998n⌋+⌊999n⌋+⌊1000n⌋\left\lfloor \frac{998}{n} \right\rfloor + \left\lfloor \frac{999}{n} \right\rfloor + \left\lfloor \frac{1000}{n} \right\rfloor

not divisible by 33? (Recall that ⌊x⌋\lfloor x \rfloor is the greatest integer less than or equal to xx.)

Checkpoint for section 22.0a

Get 2 of 3 right to master this section. One try each, no hints.
1

What digit AA makes the four-digit number 3A723A72 divisible by 99?

2

For which digit dd is the six-digit number 73d,42573d{,}425 divisible by 1111?

3

How many of these numbers are divisible by 66:   135,  2340,  1236,  4510,  8112\;135,\; 2340,\; 1236,\; 4510,\; 8112?

22.0b0%

Legendre's Formula

In this section you will be able to count the factors of a prime p in n! with Legendre's formula, and use it to count the zeros at the end of a factorial.

How many zeros are at the end of 100!100!? You don’t need a single one of its 158158 digits. Just count the 55s.

A zero at the end is a factor of 10=2×510 = 2 \times 5. A factorial always has more 22s than 55s, so the number of zeros is the number of 55s.

To count the 55s in 100!100!, count the multiples of 55 (each gives one 55), then the multiples of 2525 (each gives a second one), and so on. That is Legendre’s formula.

Exploreplay first, formulas later

Factors of 5 in 19!: 3 = 3

n19
51015
  • Layer 1: multiples of 5 = 5 up to 19: 3
5s in 19!: 3 = 3

19! has 16 factors of 2 and 3 factors of 5. Each zero needs one 2 and one 5, so the number of zeros at the end is min(16, 3) = 3

5 in 19!: 3 = 3

Name it

Theorem 22.0.2 · Legendre's Theorem

For a prime pp, the number of factors of pp in n!n! is

vp(n!)=⌊np⌋+⌊np2⌋+⌊np3⌋+⋯v_p(n!) = \left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \left\lfloor \frac{n}{p^3} \right\rfloor + \cdots

The sum stops once pk>np^k > n, because every later term is 00.

Watch itpredict, then reveal

Example 22.2

AIME

Let NN be the number of consecutive 00's at the right end of the decimal representation of the product 1! 2! 3! 4!⋯99! 100!1!\,2!\,3!\,4! \cdots 99!\,100!. Find the remainder when NN is divided by 10001000.

Legendre stack (its total is revealed at the end of the example)

20406080100
  • Layer 1: ⌊k/5⌋ added for k = 1 … 100: ?
  • Layer 2: ⌊k/5²⌋ = ⌊k/25⌋ added for k = 1 … 100: ?
5s in 1!·2!·…·100!: 0 so far

5 in 1!·2!·…·100!: 0 so far

  1. 1

    Each zero at the end is a factor of 10=2×510 = 2 \times 5. Which prime runs out first in 1! 2!⋯100!1!\,2!\cdots100!, so it decides NN?

    Predict first:
    Click a choice or type its letter (A–E).
open on YouTube

Example 22.3

AMC 10

The base-ten representation for 19!19! is 121,6T5,100,40M,832,H00121{,}6T5{,}100{,}40M{,}832{,}H00, where TT, MM, and HH denote digits that are not given. What is T+M+HT + M + H?

Divisibility lens (its total is revealed at the end of the example)

Tap a letter to try a digit for it.

  • Digit sum 1 + 2 + 1 + 6 + T + 5 + 1 + 0 + 0 + 4 + 0 + M + 8 + 3 + 2 + H + 0 + 0 = 33 + T + M + H must be a multiple of 9.
Is 1216T510040M832H00 divisible by 9? It depends on T, M and H

Digits for T, M and H that make it divisible by 9:

? (hidden until the end of the example)

Is 1216T510040M832H00 divisible by 9? It depends on T, M and H. Digit sum 1 + 2 + 1 + 6 + T + 5 + 1 + 0 + 0 + 4 + 0 + M + 8 + 3 + 2 + H + 0 + 0 = 33 + T + M + H must be a multiple of 9.

  1. 1

    Start with HH. How many factors of 55 does 19!19! have?

    Predict first:
open on YouTube

Try it

Problem 22.1.3

AMC 10

For all integers n≥9n \ge 9, the value of

(n+2)!−(n+1)!n!\frac{(n + 2)! - (n + 1)!}{n!}

is always which of the following?

Click a choice or type its letter (A–E).

Contest cornerreal contest problems

Problem 22.1.8(additional)

AMC 12

The number 21!=51,090,942,171,709,440,00021! = 51{,}090{,}942{,}171{,}709{,}440{,}000 has over 60,00060{,}000 positive integer divisors. One of them is chosen at random. What is the probability that it is odd?

Problem 22.1.15(additional)

AIME

Define n!!n!! to be n(n−2)(n−4)⋯3⋅1n(n - 2)(n - 4) \cdots 3 \cdot 1 for nn odd and n(n−2)(n−4)⋯4⋅2n(n - 2)(n - 4) \cdots 4 \cdot 2 for nn even. When

∑i=12009(2i−1)!!(2i)!!\sum_{i=1}^{2009} \frac{(2i - 1)!!}{(2i)!!}

is expressed as a fraction in lowest terms, its denominator is 2ab2^a b with bb odd. Find ab10\dfrac{ab}{10}.

Checkpoint for section 22.0b

Get 2 of 3 right to master this section. One try each, no hints.
1

How many factors of 33 are in 30!30!? (That is, what is the largest kk with 3k∣30!3^k \mid 30!?)

2

How many zeros are at the end of 125!125!?

3

What is the largest kk such that 2k2^k divides 50!50!?

22.1

Practice Problems

In this section you will be able to mix every tool from this chapter: grid crossings, gcds, remainders mod 99 and the digit-sum trick.

These problems mix the rules. Some are repeats from earlier in the book: if you solved them there, try a different route here.

Contest cornerreal contest problems

Problem 22.1.1

AMC 10

A rectangular floor that is 1010 feet wide and 1717 feet long is tiled with 170170 one-foot square tiles. A bug walks from one corner to the opposite corner in a straight line. Including the first and the last tile, how many tiles does the bug visit?

Problem 22.1.4

IMO

Prove that the fraction 21n+414n+3\frac{21n + 4}{14n + 3} is irreducible for every natural number nn.

This one asks for a proof. Write yours on paper, use the hints if you need them, then compare it with the written proof.

Problem 22.1.9(additional)

AMC 10

This problem also appears in the book as 22.1.6.

Call a positive integer an uphill integer if every digit is strictly greater than the previous digit. For example, 13571357, 8989, and 55 are all uphill integers, but 3232, 12401240, and 466466 are not. How many uphill integers are divisible by 1515?

Problem 22.1.10(additional)

AMC 10

This problem also appears in the book as 21.3.20.

Let N=123456789101112…4344N = 123456789101112 \dots 4344 be the 7979-digit number that is formed by writing the integers from 11 to 4444 in order, one after the other. What is the remainder when NN is divided by 4545?

Problem 22.1.11(additional)

AMC 10

This problem also appears in the book as 22.1.

Let nn denote the smallest positive integer that is divisible by both 44 and 99, and whose base-10 representation consists of only 44's and 99's, with at least one of each. What are the last four digits of nn?

Problem 22.1.12(additional)

AMC 10

A finite sequence of three-digit integers has the property that the tens and units digits of each term are, respectively, the hundreds and tens digits of the next term, and the tens and units digits of the last term are, respectively, the hundreds and tens digits of the first term. For example, such a sequence might begin with the terms 247247, 475475, and 756756 and end with the term 824824. Let SS be the sum of all the terms in the sequence. What is the largest prime factor that always divides SS?

Problem 22.1.14(additional)

AIME

Let a10=10a_{10} = 10, and for each positive integer n>10n > 10 let an=100an−1+na_n = 100a_{n-1} + n. Find the least positive n>10n > 10 such that ana_n is a multiple of 9999.