Library · The Fiddler: Solutions · Chapter 150

Can You Make Something out of Nothing?

Revised Report an error

Take the two functions f(n)=2n+1f(n) = 2n + 1 and g(n)=4ng(n) = 4n, and apply them in any combination, starting from 00. For example f(0)=1f(0) = 1, f(f(0))=3f(f(0)) = 3, g(f(0))=4g(f(0)) = 4, f(f(f(0)))=7f(f(f(0))) = 7, f(g(f(0)))=9f(g(f(0))) = 9 and g(g(f(0)))=16g(g(f(0))) = 16. How many of the whole numbers from 11 to 1,0241{,}024 inclusive can be produced this way?

The Fiddler, Zach Wissner-Gross, July 28, 2023(original post)

The official solution appeared in the post of August 4, 2023 and gives 144144 for the main puzzle and 234234 for the Extra Credit. Both agree with the answers below; the 234234 counts 00 itself, which gg produces from 00.

Solution

The upper limit 1,024=2101{,}024 = 2^{10} is a hint to write numbers in binary. There, doubling a number appends a 00 to its digits, just as multiplying by ten does in decimal. So ff, which doubles and adds one, appends the digit 11, and gg, which multiplies by four, appends the two digits 0000.

Starting from 00, applying gg changes nothing, and the first ff produces 11. From then on each step appends a block, either 11 or 0000. So the numbers that can be produced, apart from 00, are exactly those whose binary digits are a leading 11 followed by a string of the blocks 11 and 0000. Different strings of blocks give different numbers, because reading the digits from the left splits them back into blocks in only one way: a 11 is a block, and a 00 must start a 0000.

How many such numbers have exactly LL binary digits? After the leading 11 there are L−1L - 1 digits to fill with blocks of length 11 and 22. Call the number of ways kLk_L. The last block is either a 11, leaving L−2L - 2 digits before it, or a 0000, leaving L−3L - 3, so kL  =  kL−1+kL−2,k1=1,k2=1.k_L \;=\; k_{L-1} + k_{L-2}, \qquad k_1 = 1, \quad k_2 = 1 . These are the Fibonacci numbers:

digits LL 1 2 3 4 5 6 7 8 9 10
numbers kLk_L 1 1 2 3 5 8 13 21 34 55

Every number with at most ten binary digits lies between 11 and 1,0231{,}023, so those contribute 1+1+2+⋯+55=1431 + 1 + 2 + \dots + 55 = 143. An eleven-digit number is at least 210=1,0242^{10} = 1{,}024, and only 1,0241{,}024 itself is in range; it is 11 followed by five blocks 0000, which is g5(f(0))g^5(f(0)). The total is  143+1  =  144 numbers. \boxed{\ 143 + 1 \;=\; 144 \text{ numbers}. \ } That 144144 is itself a Fibonacci number is no accident: the first nn Fibonacci numbers add up to one less than the (n+2)(n + 2)th, here 143=F12−1143 = F_{12} - 1.

The computation

The check makes no use of binary. It applies the functions over and over, starting from 00, and collects everything it reaches in the range. Neither function ever makes a nonzero number smaller in size, so a number that leaves the range can never come back, and the search can stop at the boundary.

def reachable(functions, low, high):
    """Numbers in [low, high] reachable from 0. No function
    shrinks a nonzero number, so nothing outside the range
    leads back into it and the search stops at the edge."""
    seen, todo = {0}, [0]
    while todo:
        n = todo.pop()
        for fn in functions:
            m = fn(n)
            if low <= m <= high and m not in seen:
                seen.add(m)
                todo.append(m)
    return seen

f = lambda n: 2*n + 1
g = lambda n: 4*n
h = lambda n: 1 - 2*n

main = reachable([f, g], 0, 1024)
print("main:", len([n for n in main if 1 <= n <= 1024]))

It prints

main: 144

Extra Credit

Now use g(n)=4ng(n) = 4n together with h(n)=1−2nh(n) = 1 - 2n. How many integers from −1,024-1{,}024 to 1,0241{,}024 inclusive can be produced from 00?

Solution

In binary, hh misbehaves: it can lose digits as well as gain them, since h(4)=−7h(4) = -7 has the same number of binary digits as 44. The cure is to change the base. Write h(n)=(−2) n+1h(n) = (-2)\,n + 1 and g(n)=(−2)2 ng(n) = (-2)^2\,n. In base −2-2, where the place values are 1,−2,4,−8,16,…1, -2, 4, -8, 16, \dots, multiplying by −2-2 appends a 00, so hh appends the digit 11 and gg appends 0000. This is the main puzzle again, with base 22 replaced by base −2-2.

Base −2-2 is a perfectly good number system. Every integer, positive or negative, has exactly one representation using the digits 00 and 11 with no minus sign. For example −7=1−8-7 = 1 - 8 is written 10011001, and 3=4−2+13 = 4 - 2 + 1 is written 111111. The sign of a number is the sign of its leading place value, because the lower places can never outweigh it: below a leading (−2)p(-2)^p, the places of the opposite sign add up to at most 23(2p−1)\frac23\bigl(2^p - 1\bigr) in size, which is less than 2p2^p.

So the reachable integers, apart from 00, are again exactly the strings made of a leading 11 followed by blocks 11 and 0000, and there are kLk_L of them with LL digits. What changes is which lengths fit the range. The LL-digit numbers lie between these bounds:

digits leading place range of values
11 to 1010 11 to −512-512 −682-682 to 341341
1111 1,0241{,}024 342342 to 1,3651{,}365
1212 −2,048-2{,}048 −2,730-2{,}730 to −683-683
1313 or more beyond ±1,024\pm 1{,}024

Every reachable number of up to ten digits is in range, and there are 143143 of them, as before. The eleven- and twelve-digit numbers need a closer look.

Eleven digits. Such a number is 1,0241{,}024 plus a ten-digit tail made of blocks, and it is in range exactly when the tail is zero or negative. The tail’s highest 11 is the first 11 block, which sits after some number jj of 0000 blocks, at position 9−2j9 - 2j counting from 00 on the right. That position is always odd, so its place value is negative, and so is the tail. All k11=89k_{11} = 89 of these numbers are in range.

Twelve digits. Such a number is −2,048-2{,}048 plus an eleven-digit tail, and it is in range only if the tail is at least 1,0241{,}024. Now the tail’s highest 11 is at an even position 10−2j10 - 2j, and the tail reaches 1,0241{,}024 only if that position is 1010, so that the tail is 1,0241{,}024 plus a ten-digit tail of the kind just seen, which is zero or negative. So the tail is exactly 1,0241{,}024 and the number is −1,024-1{,}024 itself, written 1111 followed by ten 00s, which is g5(h(h(0)))g^5(h(h(0))): one number.

The total. Adding 00, which g(0)=0g(0) = 0 produces,  1+143+89+1  =  234 integers. \boxed{\ 1 + 143 + 89 + 1 \;=\; 234 \text{ integers}. \ } If 00 is not counted as produced, the count is 233233.

The computation

The same search, now with gg and hh and the range −1,024-1{,}024 to 1,0241{,}024, also reports whether the two ends of the range were reached.

extra = reachable([g, h], -1024, 1024)
print("extra credit:", len(extra), "including 0,",
      len(extra - {0}), "without it")
print("extremes reached:", 1024 in extra, -1024 in extra)

It prints

extra credit: 234 including 0, 233 without it
extremes reached: True True
Report an error on this page

Reports are stored by Netlify. See the privacy note.