Library · The Fiddler: Solutions · Chapter 129

How Many Times Can You Add Up the Digits?

Revised Report an error

For a positive whole number NN, let f(N)f(N) be the number of times you must add up its digits before you reach a one-digit number. For example, f(23)=1f(23) = 1, since 2+3=52 + 3 = 5, and f(888)=2f(888) = 2, since 8+8+8=248 + 8 + 8 = 24 and then 2+4=62 + 4 = 6. Find the smallest whole number NN with f(N)=4f(N) = 4.

The Fiddler, Zach Wissner-Gross, February 2, 2024(original post)

The official solution appeared in the post of February 9, 2024 and gives 2×1022−12 \times 10^{22} - 1, a one followed by twenty-two nines, for the main puzzle, agreeing with the answer below. The Extra Credit half of that post is paywalled, so the count of 945945 is my own and has not been checked against it.

Solution

Write s(N)s(N) for the sum of the digits of NN. For a number with two or more digits, the first step replaces NN by s(N)s(N), and the rest of the process is just the process for s(N)s(N). So f(N)=1+f(s(N))for N≥10.f(N) = 1 + f\bigl(s(N)\bigr) \qquad\text{for } N \ge 10 . To have f(N)=4f(N) = 4, the digit sum s(N)s(N) must have f=3f = 3. So build up the smallest numbers with each value of ff, one level at a time.

  • f(N)=0f(N) = 0 means NN already has one digit: 1,2,…,91, 2, \dots, 9.

  • f(N)=1f(N) = 1 means N≥10N \ge 10 with digit sum below 1010. The smallest is 1010.

  • f(N)=2f(N) = 2 means NN’s digit sum has f=1f = 1, so it is at least 1010. The smallest number with digit sum 1010 is 1919, and indeed 1+9=101 + 9 = 10, then 1+0=11 + 0 = 1.

  • f(N)=3f(N) = 3 means NN’s digit sum has f=2f = 2. Looking at the numbers with f=2f = 2, they are 19,28,29,37,38,39,…19, 28, 29, 37, 38, 39, \dots, so the digit sum must be at least 1919. The smallest number with digit sum 1919 is 199199.

Now the step that matters. If f(N)=4f(N) = 4, then f(s(N))=3f(s(N)) = 3, and every number with f=3f = 3 is at least 199199, as just shown. So the digits of NN add up to at least 199199. Each digit is at most 99, and 22×9=19822 \times 9 = 198, so NN needs at least 2323 digits. The smallest 2323-digit candidate starts with a 11, and then its other 2222 digits must supply at least 198198, which forces them all to be 99. That number is N=199…9⏟22 nines=2×1022−1,N = 1\underbrace{99\ldots9}_{22 \text{ nines}} = 2 \times 10^{22} - 1 , and it works: its digit sum is 1+22×9=1991 + 22 \times 9 = 199, and 199→19→10→1199 \to 19 \to 10 \to 1 takes three more steps.  N=2×1022−1=19,999,999,999,999,999,999,999. \boxed{\ N = 2 \times 10^{22} - 1 = 19{,}999{,}999{,}999{,}999{,}999{,}999{,}999. \ } The same climb gives the next record. The smallest NN with f(N)=5f(N) = 5 has digit sum 2×1022−12 \times 10^{22} - 1, so it is a 11 followed by (2×1022−2)/9(2 \times 10^{22} - 2)/9 nines, about 2.2×10212.2 \times 10^{21} of them.

The computation

The program applies the definition directly. It finds by search the smallest number with each value of ff up to 33, confirms that no digit sum below 199199 has f=3f = 3, and checks the answer.

def f(n):
    steps = 0
    while n >= 10:
        n = sum(map(int, str(n)))
        steps += 1
    return steps

for k in range(4):
    print(f"smallest with f = {k}:",
          next(n for n in range(1, 10**4) if f(n) == k))

N = 2 * 10**22 - 1
print("N =", N, " f(N) =", f(N), " digits:", len(str(N)))

It prints

smallest with f = 0: 1
smallest with f = 1: 10
smallest with f = 2: 19
smallest with f = 3: 199
N = 19999999999999999999999  f(N) = 4  digits: 23

Because the smallest number with f=3f = 3 is 199199, any NN with f(N)=4f(N) = 4 needs digit sum at least 199199 and so at least 2323 digits, and the printed NN is the smallest such 2323-digit number.

Extra Credit

For how many whole numbers NN from 11 to 10,00010{,}000 is f(N)=3f(N) = 3?

Solution

The number 10,00010{,}000 has digit sum 11, so f=1f = 1 and it does not count. Every other NN in range has at most four digits, so its digit sum is at most 3636. We need f(s(N))=2f(s(N)) = 2. Among 11 to 3636, the numbers with f=2f = 2 are those whose digits add up to 1010 or more: 1919, 2828 and 2929 (the next, 3737, is too big). So we must count the four-digit strings d1d2d3d4d_1 d_2 d_3 d_4 (leading zeros allowed, since they do not change the digit sum) with digit sum 1919, 2828 or 2929.

Counting strings of four digits with a given sum is a stars-and-bars problem with a cap of 99 on each digit. Without the cap, the number of ways to write s=d1+d2+d3+d4s = d_1 + d_2 + d_3 + d_4 with each di≥0d_i \ge 0 is (s+33)\binom{s + 3}{3}. A string in which some digit is 1010 or more can be counted by taking 1010 off that digit, so inclusion and exclusion gives c(s)=(s+33)−4(s−73)+6(s−173)−⋯ ,c(s) = \binom{s+3}{3} - 4\binom{s-7}{3} + 6\binom{s-17}{3} - \cdots, with terms dropping out once the top number is below 33.

  • For s=19s = 19: c=(223)−4(123)=1540−880=660c = \binom{22}{3} - 4\binom{12}{3} = 1540 - 880 = 660.

  • For s=28s = 28 and 2929, replace each digit dd by 9−d9 - d. This turns digit sum ss into 36−s36 - s, one for one, so c(28)=c(8)=(113)=165c(28) = c(8) = \binom{11}{3} = 165 and c(29)=c(7)=(103)=120c(29) = c(7) = \binom{10}{3} = 120.

Adding,  660+165+120  =  945 numbers. \boxed{\ 660 + 165 + 120 \;=\; 945 \text{ numbers}. \ }

The computation

With the function ff from The computation above, simply count.

print(sum(1 for n in range(1, 10_001) if f(n) == 3))

It prints

945
Report an error on this page

Reports are stored by Netlify. See the privacy note.