Tag Archives: python

327273 + 327274 + … + 872728 = 327273872728: A Puzzle From Ben Vitale


On December 31st, I saw the following puzzle by Ben Vitale: if we consider the sum of consecutive integers like so \(a_{1} + a_{2} + \cdots + a_{n}\), then for which \(a_{1}\) and \(a_{n}\) do we have that our sum equals the concatenation of \(a_{1}\) with \(a_{n}\)? Here’s an example

$$33 + 34 + \cdots + 88 = 3388$$

Notice that \(a_{1} = 33\) and \(a_{n} = 88\) and that the sum is just gluing together 33 and 88 (concatenation). We can verify that $$\sum_{k = 33}^{88}k = 3388$$ by recognizing that $$\sum_{k=33}^{88}k = \sum_{k=1}^{88}k – \sum_{k=1}^{32}k$$ and using $$\sum_{k=1}^{N}k = \frac{N(N+1)}{2}$$

(I will use this idea later, so just keep it in the back of your mind as you read on.)

This gives $$\frac{88\cdot 89}{2} – \frac{32\cdot 33}{2}$$ which equals 3388.

Here’s another example that’s much larger:
$$1423912 + 1423913 + \cdots + 16935464 = 142391216935464$$

Notice that the sum is just the concatenation of 1423912 and 16935464.

This is fun!

One of the reasons that I liked this puzzle is that solving it requires no more mathematics than what is taught in an Algebra II course (or depending on the curriculum, Precalculus). When I first saw the puzzle and some of Ben’s sample solutions, I was fascinated. It’s a cute problem.

I scribbled my thoughts on paper and had what I felt was a working solution. But the only way to generate solutions was to program it or do the tedious arithmetic by hand. And, programming was a less lazy solution than doing hand arithmetic.

Generating Solutions

Here I’ll be a little bit more formal. I will use the “&” character to denote concatenation. Thus \(16\&17 = 1617\). We want to find \(a_{1}\) and \(a_{n}\) such that with \(0 < a_{1} < a_{n}\) we have $$a_{1} + a_{2} + \cdots + a_{n} = a_{1}\&a_{n}$$ where \(a_{k} = a_{k-1} + 1\) for \(k = 2, \ldots, n\). The trickiest part is probably to figure out how to work with concatenation. But we can see that $$a_{1}\&a_{n} = a_{1}10^{D} + a_{n}$$ for some integer \(D\). For example, \(16\&17 = 16\cdot 10^{2} + 17\) and \(12\&345 = 12\cdot 10^{3} + 345\). In other words, \(D\) is the just the number of digits of \(a_{n}\). Therefore, if we express \(a_{n}\) as a power of ten, \(a_{n} = 10^{p}\) then, \(p = \frac{\log(a_{n})}{\log(10)}\) where \(\log\) is in whatever sensible base you want. And thus, the number of digits \(D\) is just \(D = \left \lceil{p}\right \rceil\) (the first integer that is greater than or equal to \(p\)). Next, we know that $$a_{1} + a_{2} + \cdots + a_{n} = \frac{a_{n}(a_{n} + 1) - a_{1}(a_{1}-1)}{2}$$ since \(a_{1}\) to \(a_{n}\) are consecutive integers by the problem statement. Now, since we have that $$a_{1} + a_{2} + \cdots + a_{n} = a_{1}\&a_{n}$$ then putting everything together we have $$\frac{a_{n}(a_{n} + 1) - a_{1}(a_{1}-1)}{2} = a_{1}10^{D} + a_{n}$$ remembering that \(D\) is a function of \(a_{n}\). Once we rearrange terms and multiply both sides of the equation by 2 we have $$a_{1}^{2} + \Big(-1+2\cdot 10^{D}\Big)a_{1} + -a_{n}^{2} + a_{n} = 0$$ This is of the form $$Aa_{1}^{2} + Ba_{1} + C = 0$$ with \(A = 1\), \(B = -1+2\cdot 10^{D}\) and \(C = -a_{n}^{2} + a_{n}\). The left hand side is a quadratic in \(a_{1}\) and so by the quadratic formula and only allowing for the positive solution we have that \(a_{1} = \frac{-B + \sqrt{B^{2} - 4C}}{2}\). If \(B^{2} - 4C\) is a perfect square and if \(-B + \sqrt{B^{2}-4C}\) is divisible by 2, then we have a solution! But! There's one more thing, \(B\) and \(C\) are functions of \(a_{n}\). Thus, to generate solutions, we just have to iterate over \(a_{n}\). And this is where the programming comes into play.

A Simple Python Program

Now that it’s worked out on paper, here’s a simple implementation in Python. Probably not the most robust, but I’m also not going to go nuts with it. I report both solutions to the quadratic but when I print out solutions I only use the positive solution, of course.


>>> import math
>>> def digitconcat(n):
        D = int(math.ceil(math.log(n)/math.log(10)))
        if 10**D == n:
                D = D + 1
        B = -1 + 2*10**D
        C = -(n**2) + n
        if B**2-4*C >= 0:
                d = math.sqrt(B**2-4*C)
                a1 = -B + int(d)
                a2 = -B - int(d)
                if int(d)**2 == B**2-4*C and a1%2 == 0:
                        return a1//2, a2//2
                return False, B, C, d, a1, a2
        return False,

Solutions

Below are solutions I found for \(0 < a_{1} < a_{n} < 20000000\). Neat, right? This is math for fun! And the more math we know, the more fun we can have with it! If I write the solution as \((a_{1}, a_{n}, a_{1}\&a_{n})\) to represent \(a_{1} + a_{2} + \cdots + a_{n} = a_{1}\&a_{n}\), then we can see some fun patterns like \((1,5,15);(13,53,1353); (133,533,133533); (1333,5333,13335333)\), etc. Some patterns skip a few powers of ten \((33,88,3388);(3273, 8728, 32738728); (327273,872728, 327273872728)\). And while it is not shown below, I can guess that the next one in the pattern would be \((32727273,87272728,3272727387272728)\) which I verify with my digitconcat function. It’s fun to get lost in the patterns. What is it about 27s sandwiched between 3s, and 72s sandwiched between 8s that makes this work?

One note: on Ben’s website you will find solutions like \(19 + 18 + \cdots + 1 + 0 = 190\). I didn’t consider this case, but it is equally doable.

A big thanks to Ben for sharing this puzzle. Please check out his blog. He has many such puzzles — some are more maddening than others. I’m glad I finally had some vacation time to do some recreational math!