Library · The Fiddler: Solutions · Chapter 129
How Many Times Can You Add Up the Digits?
For a positive whole number , let be the number of times you must add up its digits before you reach a one-digit number. For example, , since , and , since and then . Find the smallest whole number with .
The Fiddler, Zach Wissner-Gross, February 2, 2024(original post)
The official solution appeared in the post of February 9, 2024 and gives , 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 is my own and has not been checked against it.
Solution
Write for the sum of the digits of . For a number with two or more digits, the first step replaces by , and the rest of the process is just the process for . So To have , the digit sum must have . So build up the smallest numbers with each value of , one level at a time.
means already has one digit: .
means with digit sum below . The smallest is .
means ’s digit sum has , so it is at least . The smallest number with digit sum is , and indeed , then .
means ’s digit sum has . Looking at the numbers with , they are , so the digit sum must be at least . The smallest number with digit sum is .
Now the step that matters. If , then , and every number with is at least , as just shown. So the digits of add up to at least . Each digit is at most , and , so needs at least digits. The smallest -digit candidate starts with a , and then its other digits must supply at least , which forces them all to be . That number is and it works: its digit sum is , and takes three more steps. The same climb gives the next record. The smallest with has digit sum , so it is a followed by nines, about of them.
The computation
The program applies the definition directly. It finds by search the smallest number with each value of up to , confirms that no digit sum below has , 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 is , any with needs digit sum at least and so at least digits, and the printed is the smallest such -digit number.
Extra Credit
For how many whole numbers from to is ?
Solution
The number has digit sum , so and it does not count. Every other in range has at most four digits, so its digit sum is at most . We need . Among to , the numbers with are those whose digits add up to or more: , and (the next, , is too big). So we must count the four-digit strings (leading zeros allowed, since they do not change the digit sum) with digit sum , or .
Counting strings of four digits with a given sum is a stars-and-bars problem with a cap of on each digit. Without the cap, the number of ways to write with each is . A string in which some digit is or more can be counted by taking off that digit, so inclusion and exclusion gives with terms dropping out once the top number is below .
For : .
For and , replace each digit by . This turns digit sum into , one for one, so and .
Adding,
The computation
With the function from The computation above, simply count.
print(sum(1 for n in range(1, 10_001) if f(n) == 3))
It prints
945