Math Core

Module 3.5 · Number Theory

Digits and bases

Digit problems look like puzzles, but they are really algebra and modular arithmetic in disguise. The trick is always to write the number out as a polynomial in its base, a‾ b‾ c‾=100a+10b+c\underline{a}\,\underline{b}\,\underline{c} = 100a + 10b + c, and then use two facts: the digit sum controls the remainder mod b−1b - 1, and digits are small, which gives tight bounds.

Base bb as a polynomial

A number written dk‾⋯d1‾ d0‾\underline{d_k} \cdots \underline{d_1}\,\underline{d_0} in base bb means

N=dkbk+⋯+d1b+d0,0≤di≤b−1,dk≠0.N = d_k b^k + \cdots + d_1 b + d_0, \qquad 0 \le d_i \le b - 1, \quad d_k \ne 0.

This representation exists and is unique: d0d_0 is forced to be N mod bN \bmod b, then repeat with ⌊N/b⌋\lfloor N / b \rfloor. A number NN has exactly k+1k + 1 digits in base bb when bk≤N<bk+1b^k \le N < b^{k+1}, i.e. k=⌊log⁡bN⌋k = \lfloor \log_b N \rfloor.

Problems like "3‾ 5‾ 2‾b=2⋅1‾ 6‾ 5‾b\underline{3}\,\underline{5}\,\underline{2}_b = 2 \cdot \underline{1}\,\underline{6}\,\underline{5}_b" become polynomial equations in bb. Remember that every digit must be less than bb.

Digit sums and remainders

Digit sums

Let sb(N)s_b(N) be the sum of the base-bb digits of NN. Then

N≡sb(N)(modb−1),N≡d0−d1+d2−⋯(modb+1).N \equiv s_b(N) \pmod{b - 1}, \qquad N \equiv d_0 - d_1 + d_2 - \cdots \pmod{b + 1}.

In base 1010: N≡S(N)(mod9)N \equiv S(N) \pmod 9 and N≡N \equiv alternating digit sum (mod11)\pmod{11}.

Why. b≡1(modb−1)b \equiv 1 \pmod{b-1}, so every bi≡1b^i \equiv 1 and N=∑dibi≡∑diN = \sum d_i b^i \equiv \sum d_i. Similarly b≡−1(modb+1)b \equiv -1 \pmod{b+1}, so bi≡(−1)ib^i \equiv (-1)^i.

Two consequences you will use constantly:

  • N−S(N)N - S(N) is always a multiple of 99.
  • S(N)S(N) is small. A number below 10k10^k has digit sum at most 9k9k. So in an equation like n+S(n)=2026n + S(n) = 2026, you know nn is within 2828 or so of 20262026.

Carries

When you add two numbers digit by digit, each carry replaces 1010 in one column by 11 in the next, so it lowers the total digit sum by 99:

S(a+b)=S(a)+S(b)−9⋅(number of carries).S(a + b) = S(a) + S(b) - 9 \cdot (\text{number of carries}).

For doubling, a digit dd produces a carry exactly when d≥5d \ge 5 (and a carry never causes a second one, since 2d+1≤192d + 1 \le 19). So S(2n)=2S(n)−9cS(2n) = 2S(n) - 9c, where cc is the number of digits of nn that are at least 55. The same carry counting in base pp is the heart of Kummer's theorem in the pp-adic module.

Trailing zeros in base bb

The number of trailing zeros of NN in base bb is the largest kk with bk∣Nb^k \mid N. For b=p1e1⋯b = p_1^{e_1} \cdots, that is the minimum over primes pi∣bp_i \mid b of ⌊vpi(N)/ei⌋\left\lfloor v_{p_i}(N) / e_i \right\rfloor, where vp(N)v_p(N) is the exponent of pp in NN. For factorials, vp(n!)v_p(n!) comes from Legendre's formula.

Worked example: An equation in the base

Is there a base bb in which 2‾ 4‾ 1‾b=3⋅5‾ 2‾b\underline{2}\,\underline{4}\,\underline{1}_b = 3 \cdot \underline{5}\,\underline{2}_b?

2b2+4b+1=3(5b+2)=15b+62b^2 + 4b + 1 = 3(5b + 2) = 15b + 6, so 2b2−11b−5=02b^2 - 11b - 5 = 0, (2b+1)(b−5)=0(2b + 1)(b - 5) = 0, b=5b = 5. But the digit 55 is not allowed in base 55. So there is no such base. Always check the digit constraint.

Worked example: Digit algebra

A two-digit number is 44 times the sum of its digits. What can it be?

10a+b=4(a+b)⇒6a=3b⇒b=2a10a + b = 4(a + b) \Rightarrow 6a = 3b \Rightarrow b = 2a. So the numbers are 12,24,36,4812, 24, 36, 48.

Worked example: Digit sum plus bounding

Find all nn with n+S(n)=1000n + S(n) = 1000.

Mod 99: n+S(n)≡2n≡1000≡1n + S(n) \equiv 2n \equiv 1000 \equiv 1, so n≡5(mod9)n \equiv 5 \pmod 9. Also n<1000n < 1000 has S(n)≤27S(n) \le 27, so n≥973n \ge 973. The numbers ≡5(mod9)\equiv 5 \pmod 9 from 973973 to 999999 are 977,986,995977, 986, 995. Check: 977+23=1000977 + 23 = 1000 ✓, 986+23=1009986 + 23 = 1009, 995+23=1018995 + 23 = 1018. So n=977n = 977 only.

Worked example: How many digits

How many digits does 21002^{100} have?

log⁡102100=100log⁡102≈30.103\log_{10} 2^{100} = 100 \log_{10} 2 \approx 30.103, so 1030≤2100<103110^{30} \le 2^{100} < 10^{31}: it has 3131 digits.

Common mistake

The mod-99 trick only gives a necessary condition. It narrows the candidates; you still have to check each one, as in the n+S(n)=1000n + S(n) = 1000 example, where two of the three candidates failed.

Practice

Practice 1

Find the base bb for which 3‾ 5‾ 2‾b=2⋅1‾ 6‾ 5‾b\underline{3}\,\underline{5}\,\underline{2}_b = 2 \cdot \underline{1}\,\underline{6}\,\underline{5}_b.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

Find the three-digit positive integer that equals 1111 times the sum of its digits.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

How many zeros does 100!100! end with when written in base 1212?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

Let S(n)S(n) denote the sum of the decimal digits of nn. How many positive integers nn satisfy n+S(n)+S(S(n))=2025n + S(n) + S(S(n)) = 2025?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

The number N=123456789101112…20252026N = 123456789101112\ldots20252026 is formed by writing the integers from 11 to 20262026 in order. Find the remainder when NN is divided by 3636.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

How many positive integers less than 10001000 have no digit 22 when written in base 33?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Let S(n)S(n) be the sum of the decimal digits of nn. How many positive integers n≤1000n \le 1000 satisfy S(n)=S(2n)S(n) = S(2n)?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice