The Universe of Discourse
https://blog.plover.com
The Universe of Discourse (Mark Dominus Blog)enMiscellanea about 24 puzzles
https://blog.plover.com/2017/08/28#24-puzzle-3
<p>This is a collection of leftover miscellanea about twenty-four
puzzles. In case you forgot what that is:</p>
<blockquote>
<p>The puzzle «4 6 7 9 ⇒ 24»
means that one should take the numbers 4, 6, 7, and 9, and combine
them with the usual arithmetic operations of addition, subtraction,
multiplication, and division, to make the number 24. In this case the
unique solution is $$6\times\frac{7 + 9}{4}.$$ When the target number is
24, as it often is, we omit it and just write «4 6 7 9».</p>
<p>Prior articles on this topic:</p>
<ul>
<li><a href="https://blog.plover.com/math/17-puzzle.html">A simple but difficult arithmetic puzzle</a> (July 2016)</li>
<li><a href="https://blog.plover.com/math/24-puzzle.html">Solving twenty-four puzzles</a> (March 2017)</li>
<li><a href="https://blog.plover.com/math/24-puzzle-2.html">Recognizing when two arithmetic expressions are essentially the same</a> (August 2017)</li>
</ul>
</blockquote>
<h2>How many puzzles have solutions?</h2>
<p>For each value of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24T%24">, there are 715 puzzles
«a b c d ⇒ T».
(I discussed this digression in two more earlier articles:
<a href="https://blog.plover.com/math/increasing-sequences.html">[1]</a>
<a href="https://blog.plover.com/math/increasing-sequences-2.html">[2]</a>.)
When the target <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24T%20%3d%2024%24">, 466 of the 715 puzzles have solutions. Is this
typical? Many solutions of
«a b c d»
puzzles end with a multiplication of 6 and 4, or of 8 and 3, or
sometimes of 12 and 2—so many that one quickly learns to look for
these types of solutions right away. When <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24T%3d23%24">, there won't be
any solutions of this type, and we might expect that relatively few
puzzles with prime targets have solutions.</p>
<p>This turns out to be the case:</p>
<p><img src="https://pic.blog.plover.com/math/24-puzzle-3/scatterplot.svg" alt="ALT attribute suggestions welcome!"/></p>
<p>The <em>x</em>-axis is the target number <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24T%24">, with 0 at the left, 300 at right,
and vertical guide lines every 25. The <em>y</em> axis is the number of
solvable puzzles out of the maximum possible of 715, with 0 at the
bottom, 715 at the top, and horizontal guide lines every 100.</p>
<p>Dots representing prime number targets are colored black. Dots for
numbers with two prime factors (4, 6, 9, 10, 14, 15, 21, 22, etc.) are red;
dots with three, four, five, six, and seven prime factors are orange,
yellow, green, blue, and purple respectively.</p>
<p>Two countervailing trends are obvious: Puzzles with smaller targets
have more solutions, and puzzles with highly-composite targets have
more solutions. No target number larger than 24 has as many as 466
solvable puzzles.</p>
<p>These are only trends, not hard rules. For example, there are 156
solvable puzzles with the target 126 (4 prime factors) but only 93
with target 128 (7 prime factors). Why? (I don't know. Maybe because
there is some correlation with the number of <em>different</em> prime
factors? But 72, 144, and 216 have many solutions, and only two
different prime factors.)</p>
<p>The smallest target you can't hit is 417. The following
numbers 418 and 419
are also impossible. But there are 8 sets
of four digits that can be used to make 416 and 23 sets
that can be used to make 420. The largest target that
can be hit is obviously <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%246561%20%3d%209%e2%81%b4%24">; the largest target with two
solutions is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242916%20%3d%204%c2%b79%c2%b79%c2%b79%20%3d%206%c2%b76%c2%b79%c2%b79%24">.</p>
<p><a href="https://pic.blog.plover.com/math/24-puzzle-3/number-of-solvable-puzzles.txt">(The raw data are available
here)</a>.</p>
<p>There is a lot more to discover here. For example, from looking at
the chart, it seems that the locally-best target numbers often have
the form <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%5en3%5em%24">. What would we see if we colored the dots
according to their largest prime factor instead of according to their
number of prime factors? (I tried doing this, and it didn't look like
much, but maybe it could have been done better.)</p>
<h3>Making zero</h3>
<p>As the chart shows, 705 of the 715 puzzles of the type «a b c d ⇒ 0»,
are solvable. This suggests an interesting inverse puzzle that Toph
and I enjoyed: find four digits <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2cb%2cc%2c%20d%24"> that <em>cannot</em> be used to
make zero. (<a href="https://pic.blog.plover.com/math/24-puzzle-3/SOLUTIONS-zeroes.txt">The answers</a>).</p>
<h2>Identifying interesting or difficult problems</h2>
<p><b>(Caution: this section contains spoilers for many of the most
interesting puzzles.)</b></p>
<p>I spent quite a while trying to get the computer to rank puzzles by
difficulty, with indifferent success.</p>
<h3>Fractions</h3>
<p>Seven puzzles require the use of fractions. One of these is the
notorious «3 3 8 8» that I mentioned before. This is probably the
single hardest of this type. The other six are:</p>
<pre><code> «1 3 4 6»
«1 4 5 6»
«1 5 5 5»
«1 6 6 8»
«3 3 7 7»
«4 4 7 7»
</code></pre>
<p>(<a href="https://pic.blog.plover.com/math/24-puzzle-3/solutions-fractions.png">Solutions to these (formatted image)</a>;
<a href="https://pic.blog.plover.com/math/24-puzzle-3/solutions-fractions.txt">solutions to these (plain text)</a>)</p>
<p>«1 5 5 5» is somewhat easier than the others, but they all follow
pretty much the same pattern. The last two are pleasantly
symmetrical.</p>
<!-- Also worth mentioning in this connection is the interesting
«1 6 9 9 ⇒ 18».
Unfortunately it has two solutions, and the other one is easy.
-->
<h3>Negative numbers</h3>
<p>No puzzles require the use of negative intermediate values. This
surprised me at first, but it is not hard to see why.
Subexpressions with negative intermediate values can always
be rewritten to have positive intermediate values instead.</p>
<p>For instance,
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%20%c3%97%20%289%20%2b%20%283%20%2d%204%29%29%24"> can be rewritten as
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%20%c3%97%20%289%20%2d%20%284%20%2d%203%29%29%24">
and
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%285%20%2d%208%29%c3%97%281%20%2d9%29%24">
can be rewritten as
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%288%20%2d%205%29%c3%97%289%20%2d1%29%24">.</p>
<h3>A digression about tree shapes</h3>
<p>In one of the earlier articles I asserted that there are only two
possible shapes for the expression trees of a puzzle solution:</p>
<p align="center"><table><tr>
<td align="center"><img src="https://pic.blog.plover.com/math/24-puzzle-3/tree1.png" alt="(((a # b) # c) # d)">
<td align="center"><img src="https://pic.blog.plover.com/math/24-puzzle-3/tree2.png" alt="((a # b) # (c # d))">
<tr><td align="center">Form <i>A</i> <td align="center">Form <i>B</i>
</table></p>
<p>(Pink square nodes contain operators and green round nodes contain
numbers.)</p>
<p>Lindsey Kuper pointed out that there are five possible shapes, not
two. Of course, I was aware of this (it is a Catalan number), so what
did I mean when I said there were only two? It's because I had the
idea that any tree that wasn't already in one of those two forms could
be put into form <em>A</em> by using transformations like the ones in the
previous section.</p>
<p>For example, the expression <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%284%c3%97%28%281%2b2%29%c3%b73%29%29%24"> isn't in either form,
but we can commute the × to get the equivalent <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28%281%2b2%29%c3%b73%29%c3%974%24">, which
has form <em>A</em>. Sometimes one uses the associative laws, for example to
turn <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%c3%b7%20%28b%20%c3%97%20c%29%24"> into <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%20%c3%b7%20b%29%20%c3%b7%20c%24">.</p>
<p>But I was mistaken; not every expression can be put into either of
these forms. The expression <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%288%c3%97%289%2d%282%c2%b73%29%29%24"> is an example.</p>
<h3>Unusual intermediate values</h3>
<p>The most interesting thing I tried was to look for puzzles whose
solutions require unusual intermediate numbers.</p>
<p>For example, the puzzle «3 4 4 4» looks easy (the other puzzles
with just 3s and 4s are all pretty easy) but it is rather tricky
because its only solution goes through the unusual intermediate number
28: <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%283%20%2b%204%29%20%2d%204%24">.</p>
<p>I ranked puzzles as follows: each possible intermediate number appears in a
certain number of puzzle solutions; this is the score for that
intermediate number.
(Lower scores are better, because they represent rarer intermediate numbers.)
The score
for a single expression is the score of its rarest intermediate
value. So for example <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%283%20%2b%204%29%20%2d%204%24"> has the intermediate
values 7 and 28. 7 is extremely common, and 28 is quite unusual,
appearing in only 151 solution expressions, so <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%283%20%2b%204%29%20%2d%204%24">
receives a fairly low score of 151 because of the intermediate 28.</p>
<p>Then each puzzle received a difficulty
score which was the score of its <em>easiest</em> solution expression. For
example,
«2 2 3 8»
has two solutions, one (<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%288%2b3%29%c3%972%2b2%24">) involving the quite unusual
intermediate value 22, which has a very good score of only 79. But
this puzzle doesn't
count as difficult because it also admits the obvious solution
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%248%c2%b73%c2%b7%5cfrac22%24"> and this is the solution that gives it its extremely
bad score of 1768.</p>
<p>Under this ranking, the best-scoring twenty-four puzzles, and their
scores, were:</p>
<pre><code> «1 2 7 7» 3
* «4 4 7 7» 12
* «1 4 5 6» 13
* «3 3 7 7» 14
* «1 5 5 5» 15
«5 6 6 9» 23
«2 5 7 9» 24
«2 2 5 8» 25
«2 5 8 8» 45
«5 8 8 8» 45
«2 2 2 9» 47
* «1 3 4 6» 59
* «1 6 6 8» 59
«2 4 4 9» 151
«3 4 4 4» 151
* «3 3 8 8» 152
«6 8 8 9» 152
«2 2 2 7» 155
«2 2 5 7» 155
«2 3 7 7» 155
«2 4 7 7» 155
«2 5 5 7» 155
«2 5 7 7» 156
«4 4 8 9» 162
</code></pre>
<p>(Something is not quite right here. I think «2 5 7 7» and «2 5 5 7»
should have the same score, and I don't know why they don't. But I
don't care enough to do it over.)</p>
<p>Most of these are at least a little bit interesting. The seven
puzzles that require the use of fractions appear; I have marked them
with stars. The top item is «1 2 7 7», whose only solution goes
through the extremely rare intermediate number 49. The next items
require fractions, and the one after that is «5 6 6 9», which I found
difficult. So I think there's some
value in this procedure.</p>
<p>But is there enough value? I'm not sure. The last item on the list,
«4 4 8 9», goes through the unusual number 36. Nevertheless I don't
think it is a hard puzzle.</p>
<p>(I can also imagine that someone might see the answer to «5 6 6 9» right
off, but find «4 4 8 9» difficult. The whole exercise is subjective.)</p>
<h3>Solutions with unusual tree shapes</h3>
<p>I thought about looking for solutions that involved unusual sequences
of operations. Division is much less common than the other three
operations.</p>
<p>To get it right, one needs to normalize the form of expressions, so
that the shapes <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%20%2b%20b%29%20%2b%20%28c%20%2b%20d%29%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20%28b%20%2b%20%28c%20%2b%20d%29%29%24"> aren't
counted separately. The Ezpr library can help here. But I didn't go
that far because the preliminary results weren't encouraging.</p>
<p>There are very few expressions totaling 24 that have the form
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%c3%b7b%29%c3%b7%28c%c3%b7d%29%24">. But if someone gives you a puzzle with a solution in
that form, then <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%c3%97d%29%c3%b7%28b%c3%97c%29%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%c3%97d%29%20%c3%b7%20%28b%c3%b7c%29%24"> are also
solutions, and one or another is usually very easy to see. For
example, the puzzle «1 3 8 9» has the solution <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%288%c3%b71%29%c3%b7%283%c3%b79%29%24">,
which has an unusual form. But this is an <em>easy</em> puzzle; someone with
even a little experience will find the solution <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%248%20%c3%97%20%5cfrac93%20%c3%97%201%24">
immediately.</p>
<p>Similarly there are relatively few solutions of the form
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%c3%b7%28%28b%2dc%29%c3%b7d%29%24">, but they can all be transformed into
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%c3%97d%c3%b7%28b%2dc%29%24"> which is not usually hard to find. Consider
$$\frac 8{\left(\frac{6 - 4}6\right)}.$$ This is pretty
weird-looking, but when you're trying to solve it
one of the first things you might notice is the 8, and then you would
try to turn the rest of the digits into a 3 by solving
«4 6 6 ⇒ 3»,
at which point it wouldn't take long to think of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac6%7b6%2d4%7d%24">. Or, coming at
it from the other direction, you might see the sixes and start looking
for a way to make «4 6 8 ⇒ 4», and it wouldn't take long to think of
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac8%7b6%2d4%7d%24">. </p>
<h3>Ezpr shape</h3>
<p>Ezprs (<a href="https://blog.plover.com/math/24-puzzle-2.html">see previous article</a>)
correspond more closely than abstract syntax trees do with our
intuitive notion of how expressions ought to work, so looking at the
shape of the Ezpr version of a solution might give better results than
looking at the shape of the expression tree. For example, one might
look at the number of nodes in the Ezpr or the depth of the Ezpr.</p>
<h3>Ad-hockery</h3>
<p>When trying to solve one of these puzzles, there are a few things I
always try first. After adding up the four numbers, I then look for
ways to make <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%248%c2%b73%2c%206%c2%b74%2c%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%2412%c2%b72%24">; if that doesn't work I start
branching out looking for something of the type <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24ab%5cpm%20c%24">.</p>
<p>Suppose we take a list of all solvable puzzles, and remove all the very
easy ones: the puzzles where one of the inputs is zero, or where one of
the inputs is 1 and there is a solution of the form <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24E%c3%971%24">.</p>
<p>Then take the remainder and mark them as “easy” if they have solutions of
the form <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2bb%2bc%2bd%2c%208%c2%b73%2c%206%c2%b74%2c%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%2412%c2%b72%24">. Also eliminate puzzles
with solutions of the type <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24E%20%2b%20%28c%20%2d%20c%29%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24E%c3%97%5cleft%28%5cfrac%0acc%5cright%29%24">.</p>
<p>How many are eliminated in this way? Perhaps most? The remaining
puzzles ought to have at least intermediate difficulty, and perhaps
examining just those will suggest a way to separate them further into
two or three ranks of difficulty.</p>
<h3>I give up</h3>
<p>But by this time I have solved so many twenty-four
puzzles that I am no longer sure which ones are hard and which ones
are easy. I suspect that I have seen and tried to solve most of the
466 solvable puzzles; certainly more than half. So my brain is no
longer a reliable gauge of which puzzles are hard and which are easy.</p>
<p>Perhaps looking at puzzles with five inputs would work better for me
now. These tend to be easy, because you have more to work with. But
there are 2002 puzzles and probably some of them are hard.</p>
<h2>Close, but no cigar</h2>
<p>What's the closest you can get to 24 without hitting it exactly? The
best I could do was <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%245%c2%b75%20%2d%20%5cfrac89%24">. Then I asked the computer,
which confirmed that this is optimal, although I felt foolish when I
saw the simpler solutions that are equally good: <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%246%c2%b74%20%5cpm%5cfrac%2019%24">.</p>
<p>The paired solutions
$$5 × \left(4 + \frac79\right) < 24 < 7 × \left(4 - \frac59\right)$$
are very handsome.</p>
<h2>Phone app</h2>
<p>The search program that tells us when a puzzle has solutions is only
useful if we can take it with us in the car and ask it about license
plates. A phone app is wanted. <a href="https://studio.code.org/projects/applab/32u24dDEOBMCO1vGVVw1YA">I built one with <em>Code Studio</em></a>.</p>
<p><a href="https://studio.code.org/projects/public">Code Studio is great</a>. It has a nice web interface, and beginners can
write programs by dragging blocks around. It looks very much like <a href="https://scratch.mit.edu/">MIT's
<em>scratch</em> project</a>, which is much
better-known. But Code Studio is a much better tool than Scratch. In
Scratch, once you reach the limits of what it can do, you are stuck,
and there is no escape. In Code Studio when you drag around those
blocks you are actually
writing JavaScript underneath, and you can click a button and see and
edit the
underlying JavaScript code you have written.</p>
<p>Suppose you need to convert <code>A</code> to 1 and <code>B</code> to 2 and so on. Scratch
does not provide an <code>ord</code> function, so with Scratch you are pretty
much out of luck; your only choice is to write a 26-way if-else tree,
which means dragging around something like 104 stupid blocks. In Code
Studio, you can drop down the the JavaScript level and type in <code>ord</code>
to use the standard <code>ord</code> function. Then if you go back to blocks,
the <code>ord</code> will look like any other built-in function block.</p>
<p>In Scratch, if you want to use a data structure other than an array,
you are out of luck, because that is all there is. In Code Studio,
you can drop down to the JavaScript level and use or build any data
structure available in JavaScript.</p>
<p>In Scratch, if you want to
initialize the program with bulk data, say a precomputed table of the
solutions of the 466
twenty-four puzzles, you are out of luck. In Code Studio, you can
upload a CSV file with up to 1,000 records, which then becomes
available to your program as a data structure.</p>
<p>In summary, you spend a lot of your time in Scratch working around the
limitations of Scratch, and what you learn doing that is of very
limited applicability. Code Studio is real programming and if it
doesn't do exactly what you want out of the box, you can get what you
want by learning a little more JavaScript, which is likely to be
useful in other contexts for a long time to come.</p>
<p>Once you finish your Code Studio app, you can click a button to send
the URL to someone via SMS. They can follow the link in their phone's
web browser and then use the app.</p>
<p>Code Studio is what Scratch should have been. <a href="https://studio.code.org/projects/public">Check it out</a>.</p>
<h2>Thanks</h2>
<p>Thanks to everyone who contributed to this article, including:</p>
<ul>
<li>my daughters Toph and Katara</li>
<li>Shreevatsa R.</li>
<li>Dr. Lindsey Kuper</li>
<li>Darius Bacon</li>
<li>everyone else who emailed me</li>
</ul>
Recognizing when two arithmetic expressions are essentially the same
https://blog.plover.com/2017/08/21#24-puzzle-2
<p>[ Warning: The math formatting in the RSS / Atom feed for this article is badly mutilated. I suggest you <a href="https://blog.plover.com/math/24-puzzle-2.html">read the article on my blog</a>. ]</p>
<blockquote>
<p>In this article, I discuss “twenty-four puzzles”. The puzzle «4 6 7 9 ⇒ 24»
means that one should take the numbers 4, 6, 7, and 9, and combine
them with the usual arithmetic operations of addition, subtraction,
multiplication, and division, to make the number 24. In this case the
unique solution is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%246%c2%b7%5cfrac%7b7%20%2b%209%7d%7b4%7d%24">.</p>
<p>When the target number after the <code>⇒</code> is 24, as it often is, we omit
it and just write «4 6 7 9». Every example in this article has
target number 24.</p>
<p>This is a continuation of my previous articles on this
topic:</p>
<ul>
<li><a href="https://blog.plover.com/math/17-puzzle.html">A simple but difficult arithmetic puzzle</a> (July 2016)</li>
<li><a href="https://blog.plover.com/math/24-puzzle.html">Solving twenty-four puzzles</a> (March 2017)</li>
</ul>
</blockquote>
<p>My first cut at writing a solver for twenty-four puzzles was a straightforward
search program. It had a couple of hacks in it to cut down the search
space by recognizing that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2bE%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24E%2ba%24"> are the same, but other
than that there was nothing special about it and
<a href="https://blog.plover.com/math/24-puzzle.html">I've discussed it before</a>.</p>
<p>It would quickly and accurately report whether any particular twenty-four
puzzle was solvable, but as it turned out that wasn't quite good
enough. The original motivation for the program was this: Toph and I
play this game in the car. Pennsylvania license plates have three
letters and four digits, and if we see a license plate <code>FBV 2259</code> we
try to solve «2 2 5 9». Sometimes we can't find a solution and
then we wonder: it is because there isn't one, or is it because we
just didn't get it yet? So the searcher turned into a phone app,
which would tell us whether there was solution, so we'd know whether
to give up or keep searching.</p>
<p>But this wasn't quite good enough either, because after we would find
that first solution, say <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%c2%b7%285%20%2b%209%20%2d%202%29%24">, we would wonder: are there
any more? And here the program was useless: it would
cheerfully report that there were three, so we would rack our brains
to find another, fail, ask the program to tell us the answer, and
discover to our disgust that the three solutions it had in mind were:</p>
<p>$$
2 \cdot (5 + (9 - 2)) \\
2 \cdot (9 + (5 - 2)) \\
2 \cdot ((5 + 9) - 2)
$$</p>
<p>The computer thinks these are different, because it uses different
data structures to represent them. It represents them with an abstract
syntax tree, which means that each expression is either a single
constant, or is a structure comprising an operator and its two operand
expressions—always exactly two. The computer understands the three
expressions above as having these structures:</p>
<table align=center style="margin: auto;"><tr>
<td><img style="max-width: 100%;" src="https://pic.blog.plover.com/math/24-puzzle-2/t2.svg">
<td><img style="max-width: 100%;" src="https://pic.blog.plover.com/math/24-puzzle-2/t3.svg">
<td><img style="max-width: 100%;" src="https://pic.blog.plover.com/math/24-puzzle-2/t1.svg">
</tr></table>
<p>It's not hard to imagine that the computer could be taught to
understand that the first two trees are equivalent. Getting it to
recognize that the third one is also equivalent seems somewhat more
difficult.</p>
<h2>Commutativity and associativity</h2>
<p>I would like the computer to understand that these three expressions
should be considered “the same”. But what does “the same” mean? This
problem is of a kind I particularly like: we want the computer to do
<em>something</em>, but we're not exactly sure what that something is. Some
questions are easy to ask but hard to answer, but this is the
opposite: the real problem is to decide what question we want to ask.
Fun!</p>
<p>Certainly some of the question should involve commutativity and
associativity of addition and multiplication. If the only difference
between two expressions is that one has <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20b%24"> where the other has
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%20%2b%20a%24">, they should be considered the same; similarly <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20%28b%20%2b%0ac%29%24"> is the same expression as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%20%2b%20b%29%20%2b%20c%24"> and as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28b%20%2b%20a%29%20%2b%20c%24">
and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%20%2b%20%28a%20%2b%20c%29%24"> and so forth.</p>
<p>The «2 2 5 9» example above shows that commutativity and
associativity are not limited to addition and multiplication. There
are commutative and associative properties of subtraction also! For example,
$$a+(b-c) = (a+b)-c$$ and $$(a+b)-c = (a-c)+b.$$
There ought to be names for these laws but as far as I know there aren't. (Sure, it's
just commutativity and associativity of addition in disguise, but
nobody explaining these laws to school kids <em>ever</em> seems to point out
that subtraction can enter into it. They just observe that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%2db%29%2dc%0a%e2%89%a0%20a%2d%28b%2dc%29%24">, say “subtraction isn't associative”, and leave it at that.)</p>
<p>Closely related to these identities are operator inversion identities
like <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2d%28b%2bc%29%20%3d%20%28a%2db%29%2dc%24">, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2d%28b%2dc%29%20%3d%20%28a%2db%29%2bc%24">, and their
multiplicative analogues. I don't know names for these algebraic laws
either.</p>
<p>One way to deal with all of this would to build a complicated
comparison function for abstract syntax trees that tried to transform
one tree into another by applying these identities. A better approach
is to recognize that the data structure is over-specified. If we want
the computer to understand that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%20%2b%20b%29%20%2b%20c%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20%28b%20%2b%20c%29%24">
are the same expression, we are swimming upstream by using a data
structure that was specifically designed to capture the difference
between these expressions.</p>
<p>Instead, I invented a data structure, called an <em>Ezpr</em> (“Ez-pur”), that
can represent expressions, but in a somewhat more natural way than
abstract syntax trees do, and in a way that makes commutativity and
associativity transparent. </p>
<p>An Ezpr has a simplest form, called its “canonical” or “normal” form.
Two Ezprs represent essentially the same mathematical expression if
they have the same canonical form. To decide if two abstract syntax
trees are the same, the computer converts them to Ezprs, simplifies
them, and checks to see if resulting canonical forms are identical.</p>
<h2>The Ezpr</h2>
<p>Since associativity doesn't matter, we don't want to represent it.
When we (humans) think about adding up a long column of numbers, we
don't think about associativity because we don't add them
pairwise. Instead we use an addition algorithm that adds them all at
once in a big pile. We don't treat addition as a binary operation; we
normally treat it as an operator that adds up the numbers in a list.
The Ezpr makes this explicit: its addition operator is applied to a
list of subexpressions, not to a pair. Both <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20%28b%20%2b%20c%29%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%0a%2b%20b%29%20%2b%20c%24"> are represented as the Ezpr</p>
<pre><code> SUM [ a b c - ]
</code></pre>
<p>which just says that we are adding up <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24">, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%24">, and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24c%24">. (The
<code>-</code> sign is just punctuation; ignore it for now.)</p>
<p>Similarly the Ezpr <code>MUL [ a b c ÷ ]</code> represents the product of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24">,
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%24">, and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24c%24">. (Please ignore the <code>÷</code> sign for the time being.)</p>
<p>To handle commutativity, we want those <code>[ a b c ]</code> lists to be bags.
Perl doesn't have a built-in bag object, so instead I used arrays and
required that the array elements be in sorted order. (Exactly <em>which</em>
sorted order doesn't really matter.)</p>
<h2>Subtraction and division</h2>
<p>This doesn't yet handle subtraction and division, and the way I chose
to handle them is the only part of this that I think is at all
clever. A <code>SUM</code> object has not one but two bags, one for the positive
and one for the negative part of the expression. An expression like
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2d%20b%20%2b%20c%20%2d%20d%24"> is represented by the Ezpr:</p>
<pre><code>SUM [ a c - b d ]
</code></pre>
<p>and this is <em>also</em> the representation of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20c%20%2d%20b%20%2d%20d%24">, of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24c%20%2b%20a%0a%2d%20d%20%2d%20b%24">, of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24c%20%2d%20d%2b%20a%2db%24">, and of any other expression of the
idea that we are adding up <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24c%24"> and then deducting <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%24">
and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%24">. The <code>-</code> sign separates the terms that are added from those
that are subtracted. </p>
<p>Either of the two bags may be empty, so for example <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20b%24"> is just
<code>SUM [ a b - ]</code>.</p>
<p>Division is handled similarly. Here conventional mathematical
notation does a little bit better than in the sum case: <code>MUL [ a c ÷ b
d ]</code> is usually written as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7bac%7d%7bbd%7d%24">.</p>
<p>Ezprs handle the associativity and commutativity of subtraction and
division quite well. I pointed out earlier that subtraction has an
associative law <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%20%2b%20b%29%20%2d%20c%20%3d%20a%20%2b%0a%28b%20%2d%20c%29%24"> even though it's not usually called that.
No code is required to understand that those two expressions are
equal if they are represented as Ezprs, because they are represented
by completely identical structures:</p>
<pre><code> SUM [ a b - c ]
</code></pre>
<p>Similarly there is a commutative law for subtraction: <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20b%20%2d%20c%20%3d%20a%0a%2d%20c%20%2b%20b%24"> and once again that same Ezpr does for both.</p>
<h2>Ezpr laws</h2>
<p>Ezprs are more flexible than binary trees. A binary tree can
represent the expressions <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28a%2bb%29%2bc%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2b%28b%2bc%29%24"> but not the
expression <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2bb%2bc%24">. Ezprs can represent all three and it's easy to
transform between them. Just as there are rules for building
expressions out of simpler expressions, there are a few rules for
combining and manipulating Ezprs.</p>
<h3>Lifting and flattening</h3>
<p>The most important transformation is <em>lifting</em>, which is the Ezpr
version of the associative law. In the canonical form of an Ezpr, a
<code>SUM</code> node may not have subexpressions that are also <code>SUM</code> nodes. If
you have</p>
<pre><code> SUM [ a SUM [ b c - ] - … ]
</code></pre>
<p>you should lift the terms from the inner sum into the outer one:</p>
<pre><code> SUM [ a b c - … ]
</code></pre>
<p>effectively transforming <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2b%28b%2bc%29%24"> into <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%2bb%2bc%24">. More generally,
in</p>
<pre><code> SUM [ a SUM [ b - c ]
- d SUM [ e - f ] ]
</code></pre>
<p>we lift the terms from the inner Ezprs into the outer one:</p>
<pre><code> SUM [ a b f - c d e ]
</code></pre>
<p>This effectively transforms <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20%28b%20%2d%20c%29%20%2d%20d%20%2d%20%28e%20%2d%20f%29%29%24"> to <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20%2b%20b%20%2b%0af%20%2d%20c%20%2d%20d%20%2d%20e%24">.</p>
<p>Similarly, when a <code>MUL</code> node contains another <code>MUL</code>, we can flatten
the structure.</p>
<p>Say we are converting the expression <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%247%20%c3%b7%20%283%20%c3%b7%20%286%20%c3%97%204%29%29%24"> to an Ezpr.
The conversion function is recursive and the naïve version computes
this Ezpr:</p>
<pre><code> MUL [ 7 ÷ MUL [ 3 ÷ MUL [ 6 4 ÷ ] ] ]
</code></pre>
<p>But then at the bottom level we have a <code>MUL</code> inside a <code>MUL</code>, so the
4 and 6 in the innermost <code>MUL</code> are lifted upward:</p>
<pre><code> MUL [ 7 ÷ MUL [ 3 ÷ 6 4 ] ]
</code></pre>
<p>which represents <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac7%7b%5cfrac%7b3%7d%7b6%5ccdot%204%7d%7d%24">.
Then again we have a <code>MUL</code> inside a <code>MUL</code>, and again the
subexpressions of the innermost <code>MUL</code> can be lifted:</p>
<pre><code> MUL [ 7 6 4 ÷ 3 ]
</code></pre>
<p>which we can imagine as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7b7%c2%b76%c2%b74%7d3%24">.</p>
<p>The lifting only occurs when the sub-node has the same type as its
parent; we may not lift terms out of a <code>MUL</code> into a <code>SUM</code> or vice
versa.</p>
<h3>Trivial nodes</h3>
<p>The Ezpr <code>SUM [ a - ]</code> says we are adding up just one thing, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24">,
and so it can be eliminated and replaced with just <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24">. Similarly
<code>SUM [ - a ]</code> can be replaced with the constant <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%2da%24">, if <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24"> is a
constant. <code>MUL</code> can be handled similarly.</p>
<p>An even simpler case is <code>SUM [ - ]</code> which can be replaced by the
constant 0; <code>MUL [ ÷ ]</code> can be replaced with 1. These sometimes arise
as a result of cancellation.</p>
<h3>Cancellation</h3>
<p>Consider the puzzle «3 3 4 6».
My first solver found 49 solutions to this puzzle. One is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%283%20%2d%203%29%20%2b%20%284%20%c3%97%206%29%24">.
Another is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%284%20%2b%20%283%20%2d%203%29%29%20%c3%97%206%24">. A third is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%286%20%2b%20%283%20%2d%203%29%29%24">.</p>
<p>I think these are all the same: the solution is to multiply the 4 by
the 6, and to get rid of the threes by subtracting them to make a zero
term. The zero term can be added onto the rest of expression or to
any of its subexpressions—there are ten ways to do this—and it doesn't
really matter where.</p>
<p>This is easily explained in terms of Ezprs:
If the same subexpression appears in both of a node's bags,
we can drop it. For example,
the expression <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%284%20%2b%20%283%20%2d3%29%29%20%c3%97%206%24">
starts out as</p>
<pre><code> MUL [ 6 SUM [ 3 4 - 3 ] ÷ ]
</code></pre>
<p>but the duplicate threes in <code>SUM [ 3 4 - 3 ]</code> can be canceled, to
leave</p>
<pre><code> MUL [ 6 SUM [ 4 - ] ÷ ]
</code></pre>
<p>The sum is now trivial, as described in the previous section, so can
be eliminated and replaced with just 4:</p>
<pre><code> MUL [ 6 4 ÷ ]
</code></pre>
<p>This Ezpr records the essential feature of each of the three solutions to
«3 3 4 6» that I mentioned:
they all are multiplying the 6 by the 4, and then doing something else
unimportant to get rid of the threes.</p>
<p>Another solution to the same puzzle is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%286%20%c3%b7%203%29%20%c3%97%20%284%20%c3%97%203%29%24">.
Mathematically we would write this as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac63%c2%b74%c2%b73%24"> and we can see
this is just <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%246%c3%974%24"> again, with the threes gotten rid of by
multiplication and division, instead of by addition and subtraction.
When converted to an Ezpr, this expression becomes:</p>
<pre><code> MUL [ 6 4 3 ÷ 3 ]
</code></pre>
<p>and the matching threes in the two bags are cancelled, again leaving</p>
<pre><code> MUL [ 6 4 ÷ ]
</code></pre>
<p>In fact there aren't 49 solutions to this puzzle. There is only one,
with <a href="https://pic.blog.plover.com/math/24-puzzle-2/3346-solutions.txt">49 trivial variations</a>.</p>
<h3>Identity elements</h3>
<p>In the preceding example, many of the trivial variations on the
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%c3%976%24"> solution involved multiplying some subexpression by <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%0a33%24">. When one of the input numbers in the puzzle is a 1, one can
similarly obtain a lot of useless variations by choosing where to
multiply the 1.</p>
<p>Consider
«1 3 3 5»:
We can make 24 from <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%20%c3%97%20%283%20%2b%205%29%24">. We then have to get
rid of the 1, but we can do that by multiplying it onto any
of the five subexpressions of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%20%c3%97%20%283%20%2b%205%29%24">:</p>
<p>$$
1 × (3 × (3 + 5)) \\
(1 × 3) × (3 + 5) \\
3 × (1 × (3 + 5)) \\
3 × ((1 × 3) + 5) \\
3 × (3 + (1×5))
$$</p>
<p>These should not be considered different solutions.
Whenever we see any 1's in either of the bags of a <code>MUL</code> node,
we should eliminate them.
The first expression above, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%241%20%c3%97%20%283%20%c3%97%20%283%20%2b%205%29%29%24">, is converted to the Ezpr</p>
<pre><code> MUL [ 1 3 SUM [ 3 5 - ] ÷ ]
</code></pre>
<p>but then the 1 is eliminated from the <code>MUL</code> node leaving</p>
<pre><code> MUL [ 3 SUM [ 3 5 - ] :- ]
</code></pre>
<p>The fourth expression,
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%20%c3%97%20%28%281%20%c3%97%203%29%20%2b%205%29%24">,
is initially converted to the Ezpr</p>
<pre><code> MUL [ 3 SUM [ 5 MUL [ 1 3 ÷ ] - ] ÷ ]
</code></pre>
<p>When the 1 is eliminated from the inner <code>MUL</code>, this leaves a trivial
<code>MUL [ 3 ÷ ]</code> which is then replaced with just 3, leaving:</p>
<pre><code> MUL [ 3 SUM [ 5 3 - ] ÷ ]
</code></pre>
<p>which is the same Ezpr as before.</p>
<p>Zero terms in the bags of a <code>SUM</code> node can similarly be dropped.</p>
<h3>Multiplication by zero</h3>
<p>One final case is that <code>MUL [ 0 … ÷ … ]</code> can just be simplified to 0.</p>
<p>The question about what to do when there is a zero in the denominator
is a bit of a puzzle.
In the presence of division by zero, some of our simplification rules
are questionable. For example, when we have <code>MUL [ a ÷ MUL [ b ÷ c ]
]</code>, the lifting rule says we can simplify this to <code>MUL [ a c ÷ b
]</code>—that is, that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%20a%7b%5cfrac%20bc%7d%20%3d%20%5cfrac%7bac%7db%24">. This is correct,
except that when <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%3d0%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24c%3d0%24"> it may be nonsense, depending on what else is
going on. But since zero denominators never arise in the solution of
these puzzles, there is no issue in this application.</p>
<h2>Results</h2>
<p>The <code>Ezpr</code> module is around 200 lines of Perl code, including
everything: the function that converts abstract syntax trees to Ezprs,
functions to convert Ezprs to various notations (both <code>MUL [ 4 ÷ SUM [
3 - 2 ] ]</code> and <code>4 ÷ (3 - 2)</code>), and the two versions of the
normalization process described in the previous section. The
normalizer itself is about 35 lines.</p>
<p>Associativity is taken care of by the Ezpr structure itself, and
commutativity is not too difficult; as I mentioned, it would have been
trivial if Perl had a built-in bag structure. I find it much easier
to reason about transformations of Ezprs than abstract syntax trees.
Many operations are much simpler; for example the negation of
<code>SUM [ A - B ]</code> is simply <code>SUM [ B - A ]</code>. Pretty-printing is also easier
because the Ezpr better captures the way we write and think about
expressions.</p>
<p>It took me a while to get the normalization tuned properly, but the
results have been quite successful, at least for this problem domain.
The current puzzle-solving program reports the number of distinct
solutions to each puzzle. When it reports two different solutions,
they are really different; when it fails to support the exact solution
that Toph or I found, it reports one essentially the same. (There are
some small exceptions, <a href="#arguable">which I will discuss below</a>.)</p>
<p>Since there is no specification for “essentially the same” there is no
hope of automated testing. But we have been using the app for several
months looking for mistakes, and we have not found any.
If the normalizer failed to recognize that two expressions were
essentially similar, we would be very likely to notice: we would be
solving some puzzle, be unable to find the last of the solutions that
the program claimed to exist, and then when we gave up and saw what it was we
would realize that it was essentially the same as one of the solutions we
had found. I am pretty confident that there are no errors of this
type, but see <a href="#arguable">“Arguable points”</a> below. </p>
<p>A harder error to detect is whether the computer has erroneously
conflated two essentially dissimilar expressions. To detect this we
would have to notice that an expression was missing from the computer's solution list. I am
less confident that nothing like this has occurred, but as the months
have gone by I feel better and better about it.</p>
<p>I consider the problem of “how many solutions does this puzzle
<em>really</em> have to have?” been satisfactorily solved. There are some
edge cases, but I think we have identified them.</p>
<p><a href="https://github.com/mjdominus/24-puzzle-solver">Code for my solver is on
Github</a>. The Ezpr code
is in <a href="https://github.com/mjdominus/24-puzzle-solver/blob/master/Expr.pm">the <code>Ezpr</code> package in the <code>Expr.pm</code>
file</a>.
This code is all in the public domain.</p>
<h3>Some examples</h3>
<p>The original program claims to find 35 different solutions to «4 6 6
6». The revised program recognizes that these are of only two types:</p>
<table align=center>
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%206%20%c3%97%206%20%c3%b7%206%24"><td><tt>MUL [ 4 6 - ]</tt>
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%286%20%2d%204%29%20%c3%97%20%286%20%2b%206%29%24"><td><tt>MUL [ SUM [ 6 - 4 ] SUM [ 6 6 - ] ÷ ]
</table>
<p>Some of the variant forms of the first of those include:</p>
<p>$$
6 × (4 + (6 - 6)) \\
6 + ((4 × 6) - 6) \\
(6 - 6) + (4 × 6) \\
(6 ÷ 6) × (4 × 6) \\
6 ÷ ((6 ÷ 4) ÷ 6) \\
6 ÷ (6 ÷ (4 × 6)) \\
6 × (6 × (4 ÷ 6)) \\
(6 × 6) ÷ (6 ÷ 4) \\
6 ÷ ((6 ÷ 6) ÷ 4) \\
6 × (6 - (6 - 4)) \\
6 × (6 ÷ (6 ÷ 4)) \\
\ldots <br />
$$</p>
<p>In an even more extreme case, the original program finds 80 distinct
expressions that solve
«1 1 4 6»,
all of which are trivial variations on <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%c2%b76%24">.</p>
<p>Of the 715 puzzles, 466 (65%) have solutions; for 175 of these the
solution is unique. There are 3 puzzles with 8 solutions each
(«2 2 4 8»,
«2 3 6 9»,
and «2 4 6 8»), one with 9 solutions («2 3 4 6»), and one with 10 solutions
(«2 4 4 8»).</p>
<p>The 10 solutions for «2 4 4 8»
are as follows:</p>
<table align=center>
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%208%20%2d%202%20%c3%97%204%20%20%20%20%24"><td>SUM [ MUL [ 4 8 ÷ ] - MUL [ 2 4 ÷ ] ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%282%20%2b%208%20%2d%204%29%20%20%20%24"><td>MUL [ 4 SUM [ 2 8 - 4 ] ÷ ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%288%20%2d%204%29%20%c3%97%20%282%20%2b%204%29%20%24"><td>MUL [ SUM [ 8 - 4 ] SUM [ 2 4 - ] ÷ ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%284%20%2b%208%29%20%c3%b7%202%20%20%24"><td>MUL [ 4 SUM [ 4 8 - ] ÷ 2 ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%284%20%2d%202%29%20%c3%97%20%284%20%2b%208%29%20%24"><td>MUL [ SUM [ 4 - 2 ] SUM [ 4 8 - ] ÷ ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%248%20%c3%97%20%282%20%2b%204%2f4%29%20%20%20%20%20%24"><td>MUL [ 8 SUM [ 1 2 - ] ÷ ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%20%c3%97%204%20%c3%97%204%20%2d%208%20%20%20%20%24"><td>SUM [ MUL [ 2 4 4 ÷ ] - 8 ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%248%20%2b%202%20%c3%97%20%284%20%2b%204%29%20%20%20%24"><td>SUM [ 8 MUL [ 2 SUM [ 4 4 - ] ÷ ] - ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%2b%204%20%2b%202%20%c3%97%208%20%20%20%20%20%24"><td>SUM [ 4 4 MUL [ 2 8 ÷ ] - ]
<tr><td><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244%20%c3%97%20%288%20%2d%204%2f2%29%20%24"><td>MUL [ 4 SUM [ 8 - MUL [ 4 ÷ 2 ] ] ÷ ]
</table>
<p><a href="https://pic.blog.plover.com/math/24-puzzle-2/Solutions-24.txt">A complete listing of every essentially different solution to every
«a b c d» puzzle is available here</a>.
There are 1,063 solutions in all.</p>
<h2>Arguable points <a name="arguable"></a></h2>
<p>There are a few places where we have not completely pinned down what
it means for two solutions to be essentially the same; I think there
is room for genuine disagreement. </p>
<ol>
<li><p>Any solution involving <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%c3%972%24"> can be changed into a slightly different
solution involving <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%2b2%24"> instead. These expressions are
arithmetically different but numerically equal. For example, I
mentioned earlier that
«2 2 4 8»
has 8 solutions. But two of these are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%208%20%2b%204%20%c3%97%20%282%20%2b%202%29%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%0a%20%20%20%208%20%2b%204%20%c3%97%202%20%c3%97%202%24">. I am willing to accept these as
essentially different. Toph, however, disagrees.</p></li>
<li><p>A similar but more complex situation arises in connection with «1
2 3 7». Consider <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%c3%977%2b3%24">, which equals 24. To get a solution
to «1 2 3 7», we can replace either of the threes in <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%243%c3%977%2b3%24">
with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%281%2b2%29%24">, obtaining <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28%281%20%2b%202%29%20%c3%97%207%29%20%2b%203%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%20%283%c3%977%29%2b%281%0a%20%20%20%20%2b2%29%24">. My program considers these to be different solutions.
Toph is unsure.</p></li>
</ol>
<p>It would be pretty easy to adjust the normalization process to handle
these the other way if the user wanted that.</p>
<h2>Some interesting puzzles</h2>
<p><a href="https://pic.blog.plover.com/math/24-puzzle-2/JJZ4631.jpg"><img align=right border=0 src="https://pic.blog.plover.com/math/24-puzzle-2/JJZ4631-sm.png" /></a></p>
<p>«1 2 7 7»
has only one solution, quite unusual. <a href="https://pic.blog.plover.com/math/24-puzzle-2/1277.txt">(Spoiler)</a>
«2 2 6 7»
has two solutions, both somewhat unusual. <a href="https://pic.blog.plover.com/math/24-puzzle-2/2267.txt">(Spoiler)</a></p>
<p>Somewhat similar to
«1 2 7 7»
is
«3 9 9 9»
which also has an unusual solution. But it has two other solutions
that are less surprising. <a href="https://pic.blog.plover.com/math/24-puzzle-2/3999.txt">(Spoiler)</a></p>
<p>«1 3 8 9» has an easy solution but also a quite tricky solution.
<a href="https://pic.blog.plover.com/math/24-puzzle-2/1389.txt">(Spoiler)</a></p>
<p>One of my neighbors has the license plate <code>JJZ 4631</code>.
«4 6 3 1» is one of the more difficult puzzles.</p>
<h2>What took so long?</h2>
<p><a href="https://blog.plover.com/math/24-puzzle.html">Back in March, I wrote</a>:</p>
<blockquote>
<p>I have enough material for at least three or four more articles
about this that I hope to publish here in the coming weeks.</p>
<p>But <a href="https://blog.plover.com/math/17-puzzle.html">the previous article on this
subject</a> ended similarly, saying</p>
<blockquote>
<p>I hope to write a longer article about solvers in the next week or so.</p>
</blockquote>
<p>and that was in July 2016, so don't hold your breath.</p>
</blockquote>
<p>And here we are, five months later!</p>
<p>This article was a <em>huge</em> pain to write. Sometimes I sit down to write
something and all that comes out is dreck. I sat down to write this
one at least three or four times and it never worked. The tortured
Git history bears witness. In the end I had to abandon all my earlier
drafts and start over from scratch, writing a fresh outline in an
empty file.</p>
<p>But perseverance paid off! WOOOOO.</p>
<p>[ Addendum 20170825: I completely forgot that Shreevatsa R. wrote <a href="https://shreevatsa.wordpress.com/2016/07/20/a-simple-puzzle-with-a-foray-into-inequivalent-expressions/">a very interesting article on the same topic as this one</a>,
in July of last year soon after I published my first article in this
series. ]</p>
<p>[ Addendum 20170829: A previous version of this article used the notations <code>SUM [ … # … ]</code>
and <code>MUL [ … # … ]</code>, which I said I didn't like. Zellyn Hunter has
persuaded me to replace these with <code>SUM [ … - … ]</code> and <code>MUL
[ … ÷ … ]</code>. Thank you M. Hunter! ]</p>
<p>[ <a href="https://blog.plover.com/math/24-puzzle-3.html">Yet more on this topic!</a> ]</p>
That time I met Erdős
https://blog.plover.com/2017/08/08#erdos
<p>I should have written about this sooner, by now it has been so long
that I have forgotten most of the details.</p>
<p>I first encountered Paul Erdős in the middle 1980s at a talk by
<a href="https://www.math.nyu.edu/~pach/">János Pach</a> about almost-universal
graphs. Consider graphs with a countably infinite set of vertices.
Is there a "universal" graph <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24G%24"> such that, for any finite or
countable graph <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24">, there is a copy of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24"> inside of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24G%24">?
(Formally, this means that there is an injection from the vertices of
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24"> to the vertices of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24G%24"> that preserves adjacency.) The answer
is yes; <a href="https://en.wikipedia.org/wiki/Rado_graph">it is quite easy to construct such a
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24G%24"></a> and in fact nearly
all random graphs have this property.</p>
<p>But then the questions become more interesting. Let <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24K_%5comega%24"> be the
complete graph on a countably infinite set of vertices. Say that
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24G%24"> is “almost universal” if it includes a copy of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24"> for every
finite or countable graph <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24"> <em>except</em> those that contain a copy of
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24K_%5comega%24">. Is there an almost universal graph? Perhaps surprisingly,
no! (<a href="https://math.stackexchange.com/a/449625/25554">Sketch of
proof</a>.)</p>
<p>I enjoyed the talk, and afterward in the lobby I got to meet Ron
Graham and Joel Spencer and talk to them about their Ramsey theory
book, which I had been reading, and about a problem I was working on.
Graham encouraged me to write up my results on the problem and submit
them to <a href="https://en.wikipedia.org/wiki/Mathematics_Magazine"><em>Mathematics
Magazine</em></a>, but I
unfortunately never got around to this.
Graham was there babysitting Erdős, who was one of Pách's
collaborators, but I did not actually talk to Erdős at that time. I
think I didn't recognize him. I don't know why I was able to
recognize Graham.</p>
<p>I find the almost-universal graph thing very interesting.
<a href="https://arxiv.org/abs/1404.5757">It is still an open research area</a>.
But none of this was what I was planning to talk about. I will return
to the point. A couple of years later Erdős was to speak at the
University of Pennsylvania. He had a stock speech for general
audiences that I saw him give more than once. Most of the talk would
be a description of a lot of interesting problems, the bounties he
offered for their solutions, and the progress that had been made on
them so far. He would intersperse the discussions with the sort of
Erdősism that he was noted for: referring to the U.S. and the
U.S.S.R. as “Sam” and “Joe” respectively; his ever-growing series of
styles (Paul Erdős, P.G.O.M., A.D., etc.) and so on.</p>
<p>One remark I remember in particular concerned the $3000 bounty he
offered for proving what is sometimes known as
<a href="https://en.wikipedia.org/wiki/Erd%C5%91s_conjecture_on_arithmetic_progressions">the Erdős-Túran conjecture</a>:
if <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%24"> is a subset of the natural numbers, and if <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csum_%7bn%5cin%0aS%7d%5cfrac%201n%24"> diverges, then <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%24"> contains arbitrarily long arithmetic
progressions. (A special case of this is that the primes contain
arbitrarily long arithmetic progressions, which was
<a href="https://en.wikipedia.org/wiki/Green%E2%80%93Tao_theorem">proved in 2004 by Green and Tao</a>,
but which at the time was a long-standing conjecture.) Although the
$3000 was at the time the largest bounty ever offered by Erdős, he
said it was really a bad joke, because to solve the problem would
require so much effort that the per-hour payment would be minuscule.</p>
<p>I made a special trip down to Philadelphia to attend the talk, with
the intention of visiting my girlfriend at Bryn Mawr afterward. I
arrived at the Penn math building early and wandered around the halls
to kill time before the talk. And as I passed by an office with an
open door, I saw Erdős sitting in the antechamber on a small sofa. So I sat down
beside him and started telling him about my favorite graph theory
problem.</p>
<p>Many people, preparing to give a talk to a large roomful of strangers,
would have found this annoying and intrusive. Some people might not want to
talk about graph theory with a passing stranger. But most people are
not Paul Erdős, and I think what I did was probably just the right
thing; what you <em>don't</em> do is sit next to Erdős and then ask how his flight
was and what he thinks of recent politics. We talked about my problem, and to
my great regret I don't remember any of the mathematical details of
what he said. But he did not know the answer offhand, he was not able
solve it instantly, and he did say it was interesting. So! I had a
conversation with Erdős about graph theory that was not a waste of his
time, and I think I can count that as one of my lifetime
accomplishments.</p>
<p>After a little while it was time to go down to the auditorium for the
the talk, and afterward one of the organizers saw me, perhaps
recognized me from the sofa, and invited me to the guest dinner, which
I eagerly accepted. At the dinner, I was thrilled because I secured a
seat next to Erdős! But this was a beginner mistake: he fell asleep
almost immediately and slept through dinner, which, I learned later,
was completely typical.</p>
<!--
[ From searching of my email archives, I have learned that the first
episode took place on Friday, 5 January 1990, and I now think the two
episodes took place in the reverse order, with the dinner
happening in the late 1980s as I said. ]
-->
Base-4 fractions in Telugu
https://blog.plover.com/2017/06/15#telugu
<p>Rik Signes brought to my attention that since version 5.1 Unicode has
contained the following excitingly-named characters:</p>
<pre><code> 0C78 ౸ TELUGU FRACTION DIGIT ZERO FOR ODD POWERS OF FOUR
0C79 ౹ TELUGU FRACTION DIGIT ONE FOR ODD POWERS OF FOUR
0C7A ౺ TELUGU FRACTION DIGIT TWO FOR ODD POWERS OF FOUR
0C7B ౻ TELUGU FRACTION DIGIT THREE FOR ODD POWERS OF FOUR
0C7C ౼ TELUGU FRACTION DIGIT ONE FOR EVEN POWERS OF FOUR
0C7D ౽ TELUGU FRACTION DIGIT TWO FOR EVEN POWERS OF FOUR
0C7E ౾ TELUGU FRACTION DIGIT THREE FOR EVEN POWERS OF FOUR
</code></pre>
<p>I looked into this a little and found out what they are for. It makes
a lot of sense! The details were provided by
<a href="https://pic.blog.plover.com/math/telugu/Venna.pdf">“Telugu Measures and Arithmetic Marks” by Nāgārjuna Venna</a>.</p>
<p>Telugu is the third-most widely spoken language in India, spoken
mostly in the southeast part of the country. Traditional Telugu units
of measurement are often divided into four or eight subunits. For
example, the <em>tūmu</em> is divided into four <em>kuṁcamulu</em>, the <em>kuṁcamulu</em>,
into four <em>mānikalu</em>, and the <em>mānikalu</em> into four <em>sōlalu</em>. </p>
<p>These days they mainly use liters like everyone else. But the
traditional measurements are mostly divided into fours, so amounts are
written with a base-10 integer part and a base-4 fractional part. The
characters above are the base-4 fractional digits.</p>
<p>To make the point clearer, I hope, let's imagine that we are using the
Telugu system, but with the familar western-style symbols 0123456789
instead of the Telugu digits
౦౧౨౩౪౫౬౭౮౯.
(The Telugu had theirs first
of course.) And let's use <strong>0-=Z</strong> as our base-four fractional digits,
analogous to Telugu ౦౼౽౾. (As in Telugu, we'll use the same zero
symbol for both the integer and the fractional parts.) Then to write
the number of gallons (7.4805195) in a cubic foot, we say</p>
<blockquote>
<p><strong>7.-Z=Z0</strong></p>
</blockquote>
<p>which is 7 gallons plus one (<strong>-</strong>) quart plus three (<strong>Z</strong>) cups plus two
(<strong>=</strong>) quarter-cups plus three (<strong>Z</strong>) tablespoons plus zero (<strong>0</strong>) drams,
a total of 7660 drams almost exactly. Or we could just round off to
<strong>7.=</strong>, seven and a half gallons.</p>
<p>(For the benefit of readers who might be a bit rusty on the details of
these traditional European measurements, I should mention that there
are four drams in a tablespoon, four tablespoons in a quarter cup, four
quarter cups in a cup, four cups in a quart, and four quarts in a
gallon, so 4⁵ = 1024 drams in a gallon and 7.4805195·4⁵ = 7660.052
drams in a cubic foot. Note also that these are volume (fluid) drams,
not mass drams, which are different.)</p>
<p>We can omit the decimal point (as the Telegu did) and write</p>
<blockquote>
<p><strong>7-Z=Z0</strong></p>
</blockquote>
<p>and it is still clear where the integer part leaves off and the
fraction begins, because we are using special symbols for the
fractional part. But no, this isn't quite enough, because if we wrote
<strong>20ZZ=</strong> it might not be clear whether we meant <strong>20.ZZ=</strong> or <strong>2.0ZZ=</strong>.</p>
<p>So the system has an elaboration. In the <em>odd</em> positions, we don't
use the <strong>0-=Z</strong> symbols; we use <strong>Q|HN</strong> instead. And we don't write <strong>7-Z=Z0</strong>,
we write</p>
<blockquote>
<p><strong>7|ZHZQ</strong></p>
</blockquote>
<p>This is always unambiguous: <strong>20.ZZ=</strong> is actually written <strong>20NZH</strong>
and <strong>2.0ZZ=</strong> is written <strong>2QZN=</strong>, quite different.</p>
<p>This is all fanciful in English, but Telugu actually did this.
Instead of <strong>0-=Z</strong> they had ౦౼౽౾ as I mentioned before. And instead
of <strong>Q|HN</strong> they had ౸౹౺౻. So if the Telugu were trying to write
7.4805195, where we had <strong>7|ZHZQ</strong> they might have written
౭౹౾౺౾౸. Like us, they then appended an abbreviation for the unit of
measurement. Instead of “gal.” for gallon they might have put ఘ
(letter “gha”), so ౭౹౾౺౾౸ఘ. It's all reasonably straightforward, and
also quite sensible. If you have ౭౹౾౺ <em>tūmu</em>, you can read off
instantly that there are ౺ (two) <em>sōlalu</em> left over, just as you can
see that $7.43 has three pennies left over.</p>
<p>Notice that both sets of Telugu fraction digits are easy to remember:
the digits for 3 have either three horizonal strokes ౾ or three
vertical strokes ౻, and the others similarly.</p>
<p>I have an idea that the alternating vertical-horizontal system might
have served as an error-detection mechanism: if a digit is omitted,
you notice right away because the next symbol is wrong.</p>
<p>I find this delightful. A few years back I read all of <a href="http://www.gutenberg.org/ebooks/16449"><em>The Number
Concept: Its Origin and
Development</em></a> (1931) by Levi
Leonard Conant, hoping to learn something really weird, and I was
somewhat disappointed. Conant spends most of his book describing the
number words and number systems used by dozens of cultures and almost
all of them are based on ten, and a few on five or twenty. (“Any
number system which passes the limit 10 is reasonably sure to have
either a quinary, a decimal, or a vigesimal structure.”) But he does
not mention Telugu!</p>
Solving twenty-four puzzles
https://blog.plover.com/2017/03/05#24-puzzle
<p><a href="https://blog.plover.com/math/17-puzzle.html">Back in July, I wrote</a>:</p>
<blockquote>
<p>Lately my kids have been interested in puzzles of this type: You are
given a sequence of four digits, say 1,2,3,4, and your job is to
combine them with ordinary arithmetic operations (+, -, ×, and ÷) in any order to
make a target number, typically 24. For example, with 1,2,3,4, you
can go with $$((1+2)+3)×4 = 24$$ or with $$4×((2×3)×1) = 24.$$</p>
</blockquote>
<p>I said I had found an unusually difficult puzzle of this type, which
is to make 2,5,6,6 total to 17. This is rather difficult. (I will
reveal the solution later in this article.) Several people
independently wrote to advise me that it is even more difficult to
make 3,3,8,8 total to 24. They were right; it is amazingly difficult.
After a couple of weeks I finally gave up and asked the computer, and
when I saw the answer I didn't feel bad that I hadn't gotten it
myself. (<a href="https://pic.blog.plover.com/math/24-puzzle/3388.txt">The solution is here if you want to give up without writing
a program</a>.)</p>
<p>From now on I will abbreviate the two puzzles of the previous
paragraph as «2 5 6 6 ⇒ 17» and «3 3 8 8 ⇒ 24», and others similarly.</p>
<p>The article also inspired a number of people to write their own
solvers and send them to me, and comparing them was interesting. My
solver followed the tree search technique that I described in
<a href="http://hop.perl.plover.com/book/pdf/05FromRecursionToIterators.pdf">chapter 5 of <em>Higher-Order Perl</em></a>,
and which has become so familiar to me that by now I can implement it
without thinking about it very hard:</p>
<ol>
<li><p>Invent a data structure that represents the state of a
possibly-incomplete search. This is just a list of the stuff one
needs to keep track of while searching. (Let's call this a
<em>node</em>.)</p></li>
<li><p>Build a function which recognizes when a node represents a
successful search.</p></li>
<li><p>Build a function which takes a node, computes all the ways the
search could proceed from that point, and returns a list of nodes
for those slightly-more-advanced searches.</p></li>
<li><p>Initialize a queue with a node representing a search that
has just begun.</p></li>
<li><p>Do this:</p>
<pre><code> until ( queue.is_empty() ) {
current_node = queue.get_next()
if ( is_successful( current_node ) ) { print the solution }
queue.push( slightly_more_complete_searches( current_node ) )
}
</code></pre></li>
</ol>
<p>This is precisely a breadth-first search. To make it into depth-first
search, replace the queue with a stack. To make a heuristically
directed search, replace <code>get_next</code> with a function that looks at the
queue and chooses the best-looking node from which to proceed. Many
other variations are possible, which is the advantage of this
synthetic approach over letting the search arise organically from a
recursive searcher. (<a href="http://hop.perl.plover.com/book/pdf/04Iterators.pdf"><em>Higher-Order Perl</em> says “Recursive functions
naturally perform depth-first
searches.”</a> (page
203)) In Python or Ruby one would be able to use <code>yield</code> and would
not have to manage the queue explicitly, but in this case the queue
management is trivial.</p>
<p>In my solver, each node contains a list of available
expressions, annotated with its numerical value. Initially, the
expressions are single numbers and the values are the same, say</p>
<pre><code> [ [ "2" => 2 ], [ "3" => 3 ], [ "4" => 4 ], [ "6" => 6 ] ]
</code></pre>
<p>Whether you represent expressions as strings or as something more
structured depends on what you need to do with them at the end. If
you just need to print them out, strings are good enough and are easy
to handle.</p>
<p>A node represents a successful search if it contains only a single
expression and if the expression's value is the target sum, say 24:</p>
<pre><code> [ [ "(((6÷2)+3)×4)" => 24 ] ]
</code></pre>
<p>From a node, the search should proceed by selecting two of
the expressions, removing them from the node, selecting a legal
operation, combining the two expressions into a single expression, and
inserting the result back into the node. For example, from the
initial node shown above, the search might continue by subtracting the
fourth expression from the second:</p>
<pre><code> [ [ "2" => 2 ], [ "4" => 4 ], [ "(3-6)" => -3 ] ]
</code></pre>
<p>or by multiplying the second and the third:</p>
<pre><code> [ [ "2" => 2 ], [ "(3×4)" => 12 ], [ "6" => 6 ] ]
</code></pre>
<p>When the program encounters that first node it will construct both of
these, and many others, and put them all into the queue to be
investigated later.</p>
<p>From</p>
<pre><code> [ [ "2" => 2 ], [ "(3×4)" => 12 ], [ "6" => 6 ] ]
</code></pre>
<p>the search might proceed by dividing the first expression by the third:</p>
<pre><code> [ [ "(3×4)" => 12 ], [ "(2÷6)" => 1/3 ] ]
</code></pre>
<p>Then perhaps by subtracting the first from the second:</p>
<pre><code> [ [ "((2÷6)-(3×4))" => -35/3 ] ]
</code></pre>
<p>From here there is no way to proceed, so when this node is removed
from the queue, nothing is added to replace it. Had it been a winner,
it would have been printed out, but since <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%2d%5cfrac%7b35%7d3%24"> is not the target
value of 24, it is silently discarded.</p>
<p>To solve a puzzle of the «a b c d ⇒ t» sort requires examining a few
thousand nodes. On modern hardware this takes approximately zero
seconds.</p>
<p>The actual code for my solver is a lot of Perl gobbledygook that may
not be of general interest so I will provide a link for people who are
interested in deciphering it. It also represents my second attempt: I
lost the code that I described in the earlier article and had to
rewrite it. It is rather bigger than I would have liked.</p>
<p><a href="https://pic.blog.plover.com/math/24-puzzle/mjd-solver.txt">My puzzle solver in Perl</a>.</p>
<h3>Stuff goes wrong</h3>
<p>People showed me a lot of programs to solve this, and many didn't
work. There are a few hard cases that several of them get wrong.</p>
<h4>Fractions</h4>
<p>Some puzzles require that some subexpressions have fractional values.
Many of the programs people showed me used integer arithmetic
(sometimes implicitly and unintentionally) and failed to solve those
puzzles. We can detect this by asking for a solution to «2 5 6 6 ⇒
17», which requires a fraction. The solution is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%246%c3%97%282%2b%285%c3%b76%29%29%24">. A
program using integer arithmetic will calculate <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%245%c3%b76%20%3d%200%24"> and fail
to recognize the solution.</p>
<p>Several people on Twitter made this mistake and then mistakenly
claimed that there was no solution at all. Usually it was possible to
correct their programs by changing</p>
<pre><code> inputs = [ 2, 2, 5, 6 ]
</code></pre>
<p>to</p>
<pre><code> inputs = [ 2.0, 2.0, 5.0, 6.0 ]
</code></pre>
<p>or something like that.</p>
<p>Some people also surprised me by claiming that I had lied when I
stated that the puzzle could be solved without any “underhanded
tricks”, and that the use of intermediate fractions was itself an
underhanded trick. Your Honor, I plead not guilty. I originally
described the puzzle this way:</p>
<blockquote>
<p>You are given a sequence of four digits, say 1,2,3,4, and your job is
to combine them with ordinary arithmetic operations (+, -, ×, and ÷)
in any order to make a target number, typically 24.</p>
</blockquote>
<p>The objectors are implicitly claiming that when you combine 5 and 6
with the “ordinary arithmetic operation” of division, you get
something other than <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac56%24">. This is an indefensible claim.</p>
<p>I wasn't even trying to be tricky! It never <em>occurred</em> to me that
fractions were something that some people would consider underhanded,
and now that it has been suggested, I reject the suggestion. Folks,
the result of division can be a fraction. Fractions are not some sort
of obscure mathematical pettifoggery. They have been with us for at
least 3,500 years now, so it is time everyone got used to them.</p>
<h4>Floating-point error</h4>
<p>Some programs used floating-point arithmetic to deal with the fractions and
then fell foul of floating-point error. I will defer discussion of
this to a future article.</p>
<p>I've complained about floating-point numbers on this blog before.
( <a href="https://blog.plover.com/calendar/add-month.html">1</a>
<a href="https://blog.plover.com/linogram/diophantus.html">2</a> <a href="https://blog.plover.com/math/choose.html">3</a>
<a href="https://blog.plover.com/prog/moonpig.html#fp-sucks">4</a>
<a href="https://blog.plover.com/prog/bash-expr.html">5</a> ) God, how I loathe them.</p>
<p>[ Addendum 20170825: Looking back on our old discussion from July 2016, I see that Lindsey Kuper said to me:</p>
<blockquote>
<p>One nice thing about using Racket or Scheme is that it handles the
numeric stuff so nicely. If you weren't careful, I could imagine in
Python a solution failing because it evaluated to
16.99999999999999997 or something.</p>
</blockquote>
<p>Good call, Dr. Kuper! ]</p>
<h4>Expression construction</h4>
<p>A more subtle error that several programs made was to assume that all
expressions can be constructed by combining a previous expression with
a single input number. For example, to solve «2 3 5 7 ⇒ 24», you
multiply 3 by 7 to get 21, then add 5 to get 26, then subtract 2 to
get 24.</p>
<p>But not every puzzle can be solved this way. Consider «2 3 5 7 ⇒ 41».
You start by multiplying 2 by 3 to get 6, but if you try to combine
the 6 with either 5 or 7 at this point you will lose. The only
solution is to put the 6 aside and multiply 5 by 7 to
get 35. Then add the 6 and the 35 to get 41.</p>
<p>Another way to put this is that an unordered binary tree with 4 leaves
can take two different shapes. (Imagine filling the green circles
with numbers and the pink squares with operators.)</p>
<p align="center"><table><tr>
<td><img src="https://pic.blog.plover.com/math/24-puzzle/tree1.png">
<td><img src="https://pic.blog.plover.com/math/24-puzzle/tree2.png">
</table></p>
<p>The right-hand type of structure is sometimes necessary, as with «2 3
5 7 ⇒ 41». But several of the proposed solutions produced only
expressions with structures like that on the left.</p>
<p>Here's Sebastian Fischer's otherwise very elegant Haskell solution, in
its entirety:</p>
<pre><code> import Data.List ( permutations )
solution = head
[ (a,x,(b,y,(c,z,d)))
| [a,b,c,d] <- permutations [2,5,6,6],
ops <- permutations [((+),'+'),((-),'-'),((*),'*'),((/),'/')],
let [u,v,w] = map fst $ take 3 ops,
let [x,y,z] = map snd $ take 3 ops,
(a `u` (b `v` (c `w` d))) == 17
]
</code></pre>
<p>You can see the problem in the last line. <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code> are
numbers, and <code>u</code>, <code>v</code>, and <code>w</code> are operators. The program evaluates
an expression to see if it has the value 17, but the expression always
has the left-hand shape. (The program has another limitation: it
never uses the same operator twice in the expression. That second
<code>permutations</code> should be <code>(sequence . take 3 . repeat)</code> or
something. It can still solve «2 5 6 6 ⇒ 17», however.)</p>
<p>Often the way these programs worked was to generate every possible
permutation of the inputs and then apply the operators to the input
lists stackwise: pop the first two values, combine them, push the
result, and repeat. Here's a relevant excerpt from
<a href="https://gist.github.com/tdierks/1814993d5d26d605a1de2177575dd64c">a program by Tim Dierks</a>,
this time in Python:</p>
<pre><code> for ordered_values in permutations(values):
for operations in product(ops, repeat=len(values)-1):
result, formula = calc_result(ordered_values, operations)
</code></pre>
<p>Here the expression structure is implicit, but the current result is
always made by combining <em>one</em> of the input numbers with the old
result.</p>
<p>I have seen many people get caught by this and similar traps in the
past. <a href="https://blog.plover.com/CS/parentheses.html">I once posed the problem of enumerating all the strings of
balanced parentheses of a given length</a>,
and several people assumed that all such strings have the form <code>()S</code>,
<code>S()</code>, or <code>(S)</code>, where <code>S</code> is a shorter string of the same type. This
seems plausible, and it works up to length 6, but <code>(())(())</code> does not
have that form.</p>
<h4>Division by zero</h4>
<p>A less common error exhibited by some programs was a failure to
properly deal with division by zero. «2 5 6 6 ⇒ 17» has a solution,
and if a program dies while checking <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%2b%285%c3%b7%286%2d6%29%29%24"> and doesn't find
the solution, that's a bug.</p>
<h3>Programs that worked</h3>
<h4>Ingo Blechschmidt (Haskell)</h4>
<p><a href="https://pic.blog.plover.com/math/24-puzzle/blechschmidt-solver.txt">Ingo Blechschmidt showed me a solution in
Haskell</a>. The code is quite short.
M. Blechschmidt's program defines a synthetic expression type and an
evaluator for it. It defines a function <code>arb</code> which transforms an
ordered list of numbers into a list of all possible expressions over
those numbers. Reordering the list is taken care of earlier, by
<code>Data.List.permutations</code>.</p>
<p>By “synthetic expression type” I mean this:</p>
<pre><code> data Exp a
= Lit a
| Sum (Exp a) (Exp a)
| Diff (Exp a) (Exp a)
| Prod (Exp a) (Exp a)
| Quot (Exp a) (Exp a)
deriving (Eq, Show)
</code></pre>
<p>Probably 80% of the Haskell programs ever written have something like
this in them somewhere. This approach has a lot of boilerplate. For
example, M. Blechschmidt's program then continues:</p>
<pre><code> eval :: (Fractional a) => Exp a -> a
eval (Lit x) = x
eval (Sum a b) = eval a + eval b
eval (Diff a b) = eval a - eval b
eval (Prod a b) = eval a * eval b
eval (Quot a b) = eval a / eval b
</code></pre>
<p>Having made up our own synonyms for the arithmetic operators (<code>Sum</code> for
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%2b%24">, etc.) we now have to explain to Haskell what they mean. (“Not
expressions, but an incredible simulation!”)</p>
<p>I spent a while trying to shorten the code by using a less artificial
expression type:</p>
<pre><code> data Exp a
= Lit a
| Op ((a -> a -> a), String) (Exp a) (Exp a)
</code></pre>
<p>but I was disappointed; I was only able to cut it down by 18%, from 34
lines to 28. I hope to discuss this in a future article. By the way,
“Blechschmidt” is German for “tinsmith”.</p>
<h4>Shreevatsa R. (Python)</h4>
<p><a href="https://pic.blog.plover.com/math/24-puzzle/shreevatsa.txt">Shreevatsa R. showed me a solution in Python</a>.
It generates every possible expression and prints it out with its
value. If you want to filter the voluminous output for a particular
target value, you do that later. Shreevatsa wrote up
<a href="https://shreevatsa.wordpress.com/2016/07/20/a-simple-puzzle-with-a-foray-into-inequivalent-expressions/">an extensive blog article about this</a>
which also includes a discussion about eliminating duplicate
expressions from the output. This is a very interesting topic, and I
have a lot to say about it, so I will discuss it in a future article.</p>
<h4>Jeff Fowler (Ruby)</h4>
<p><a href="https://pic.blog.plover.com/math/24-puzzle/fowler-solver.txt">Jeff Fowler of the Recurse Center wrote a compact solution in
Ruby</a> that he described as “hot
garbage”. Did I say something earlier about Perl gobbledygook? It's
nice that Ruby is able to match Perl's level of gobbledygookitude.
This one seems to get everything right, but it fails mysteriously if I
replace the floating-point constants with integer constants. He did
provide a version that was not “egregiously minified” but I don't have
it handy.</p>
<h4>Lindsey Kuper (Scheme)</h4>
<p>Lindsey Kuper wrote a series of solutions in the Racket dialect of
Scheme, and <a href="http://composition.al/blog/2016/12/31/a-simple-but-difficult-arithmetic-puzzle-and-the-rabbit-hole-it-took-me-down/">discussed them on her
blog</a>
along with some other people’s work.</p>
<p>M. Kuper's first draft was 92 lines long (counting whitespace) and
when I saw it I said “Gosh, that is way too much code” and tried
writing my own in Scheme. It was about the same size. (My Perl
solution is also not significantly smaller.)</p>
<h4>Martin Janecke (PHP)</h4>
<p>I saved the best for last. Martin Janecke showed me an almost flawless
solution in PHP that uses a completely different approach than
anyone else's program. Instead of writing a lot of code for generating
permutations of the input, M. Janecke just hardcoded them:</p>
<pre><code> $zahlen = [
[2, 5, 6, 6],
[2, 6, 5, 6],
[2, 6, 6, 5],
[5, 2, 6, 6],
[5, 6, 2, 6],
[5, 6, 6, 2],
[6, 2, 5, 6],
[6, 2, 6, 5],
[6, 5, 2, 6],
[6, 5, 6, 2],
[6, 6, 2, 5],
[6, 6, 5, 2]
]
</code></pre>
<p>Then three nested loops generate the selections of operators:</p>
<pre><code> $operatoren = [];
foreach (['+', '-', '*', '/'] as $x) {
foreach (['+', '-', '*', '/'] as $y) {
foreach (['+', '-', '*', '/'] as $z) {
$operatoren[] = [$x, $y, $z];
}
}
}
</code></pre>
<p>Expressions are constructed from templates:</p>
<pre><code> $klammern = [
'%d %s %d %s %d %s %d',
'(%d %s %d) %s %d %s %d',
'%d %s (%d %s %d) %s %d',
'%d %s %d %s (%d %s %d)',
'(%d %s %d) %s (%d %s %d)',
'(%d %s %d %s %d) %s %d',
'%d %s (%d %s %d %s %d)',
'((%d %s %d) %s %d) %s %d',
'(%d %s (%d %s %d)) %s %d',
'%d %s ((%d %s %d) %s %d)',
'%d %s (%d %s (%d %s %d))'
];
</code></pre>
<p>(I don't think those templates are all necessary, but hey, whatever.)
Finally, another set of nested loops matches each ordering of the
input numbers with each selection of operators, uses <code>sprintf</code> to plug
the numbers and operators into each possible expression template, and
uses <code>@eval</code> to evaluate the resulting expression to see if it has the
right value:</p>
<pre><code> foreach ($zahlen as list ($a, $b, $c, $d)) {
foreach ($operatoren as list ($x, $y, $z)) {
foreach ($klammern as $vorlage) {
$term = sprintf ($vorlage, $a, $x, $b, $y, $c, $z, $d);
if (17 == @eval ("return $term;")) {
print ("$term = 17\n");
}
}
}
}
</code></pre>
<p>If loving this is wrong, I don't want to be right. It certainly
satisfies Larry Wall's criterion of solving the problem before your
boss fires you. The same approach is possible in most reasonable
languages, and some unreasonable ones, but not in Haskell, which was
specifically constructed to make this approach as difficult as possible.</p>
<p><a href="https://prlbr.de/2016/6652-17/">M. Janecke wrote up a blog article about this, in
German</a>. He says “It's not an elegant
program and PHP is probably not an obvious choice for arithmetic
puzzles, but I think it works.” Indeed it does. Note that the use of
<code>@eval</code> traps the division-by-zero exceptions, but unfortunately falls
foul of floating-point roundoff errors. </p>
<h2>Thanks</h2>
<p>Thanks to everyone who discussed this with me. In addition to the
people above, thanks to Stephen Tu, Smylers, Michael Malis, Kyle
Littler, Jesse Chen, Darius Bacon, Michael Robert Arntzenius, and
anyone else I forgot. (If I forgot you and you want me to add you to
this list, please drop me a note.)</p>
<h2>Coming up</h2>
<p>I have enough material for at least three or four more articles about
this that I hope to publish here in the coming weeks.</p>
<p>But <a href="https://blog.plover.com/math/17-puzzle.html">the previous article on this
subject</a> ended similarly, saying</p>
<blockquote>
<p>I hope to write a longer article about solvers in the next week or so.</p>
</blockquote>
<p>and that was in July 2016, so don't hold your breath.</p>
<p>[ Addendum 20170820: <a href="https://blog.plover.com/math/24-puzzle-2.html">the next article</a>
is ready. I hope you weren't holding your breath! ]</p>
<p>[ Addendum 20170828: <a href="https://blog.plover.com/math/24-puzzle-3.html">yet more about this</a> ]</p>
How many 24 puzzles are there?
https://blog.plover.com/2017/02/07#increasing-sequences
<p>[ Note: The tables in this article are important, and look unusually crappy if you read this blog through an aggregator. <a href="https://blog.plover.com/math/increasing-sequences.html">The properly-formatted version on my blog</a> may be easier to follow. ]</p>
<p><a href="https://blog.plover.com/math/17-puzzle.html">A few months ago I wrote about puzzles of the following
type</a>: take four digits, say 1, 2, 7, 7,
and, using only +, -, ×, and ÷, combine them to make the number 24.
Since then I have been accumulating more and more material about these
puzzles, which will eventually appear here. But meantime here is a
delightful tangent.</p>
<p>In the course of investigating this I wrote programs to enumerate the
solutions of all possible puzzles, and these programs were always much
faster than I expected at first. It appears as if there are 10,000
possible puzzles, from «0,0,0,0» through «9,9,9,9». But a moment's
thought shows that there are considerably fewer, because, for example,
the puzzles «7,2,7,1», «1,2,7,7», «7,7,2,1», and «2,7,7,1» are all the
same puzzle. How many puzzles are there really?</p>
<p>A back-of-the-envelope estimate is that only about 1 in 24 puzzles is
really distinct (because there are typically 24 ways to rearrange the
elements of a puzzle) and so there ought to be around
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7b10000%7d%7b24%7d%20%5capprox%20417%24"> puzzles. This is an undercount,
because there are fewer duplicates of many puzzles; for example there
are not 24 variations of «1,2,7,7», but only 12. The actual number of
puzzles turns out to be 715, which I think is not an obvious thing to
guess.</p>
<p>Let's write <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2cn%29%24"> for the set of sequences
of length <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%24"> containing up to <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%24"> different
symbols, with the
duplicates removed: when
two sequences are the same except for the order of their symbols, we
will consider them the same sequence.</p>
<p>Or more concretely, we may imagine that the symbols are sorted into
nondecreasing order, so that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2cn%29%24"> is the set of nondecreasing
sequences of length <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%24"> of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%24"> different symbols.</p>
<p>Let's also write <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2cn%29%24"> for the number of elements of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2cn%29%24">.</p>
<p>Then <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%2810%2c%204%29%24"> is the set of puzzles where input is four digits.
The claim that there are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24715%24"> such puzzles is just that
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%2810%2c4%29%20%3d%20715%24">.
A tabulation of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28%5ccdot%2c%5ccdot%29%24"> reveals that it is closely related to binomial
coefficients, and indeed that
$$C(d,n)=\binom{n+d-1}{d-1}.\tag{$\heartsuit$}$$</p>
<p>so that the surprising <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24715%24"> is actually <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cbinom%7b13%7d%7b9%7d%24">. This is not hard
to prove by induction, because <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28%5ccdot%2c%5ccdot%29%24"> is easily shown to obey the same
recurrence as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cbinom%5ccdot%5ccdot%24">: $$C(d,n) = C(d-1,n) +
C(d,n-1).\tag{$\spadesuit$}$$</p>
<p>To see this, observe that an element of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2cn%29%24"> either begins with
a zero or with some other symbol. If it begins with a zero, there
are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2cn%2d1%29%24"> ways to choose the remaining <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%2d1%24"> symbols in the
sequence. But if it begins with one of the other <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%2d1%24"> symbols
it cannot contain any zeroes, and what we really have is a length-<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%24">
sequence of the symbols <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%241%5cldots%20%28d%2d1%29%24">, of which there
are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2d1%2c%20n%29%24">.</p>
<p><style>
table .rt td { padding: 2px 8px }
</style></p>
<table class="rt" style="border-collapse: collapse; border: black solid 2px; margin: 1em; float: right;">
<colgroup><col style="background: lightblue"><col style="background: pink""></colgroup>
<tr><td>0 0 0 0<td> 1 1 1
<tr><td>0 0 0 1<td> 1 1 2
<tr><td>0 0 0 2<td> 1 1 3
<tr><td>0 0 0 3<td> 1 1 4
<tr><td>0 0 1 1<td> 1 2 2
<tr><td>0 0 1 2<td> 1 2 3
<tr><td>0 0 1 3<td> 1 2 4
<tr><td>0 0 2 2<td> 1 3 3
<tr><td>0 0 2 3<td> 1 3 4
<tr><td>0 0 3 3<td> 1 4 4
<tr><td>0 1 1 1<td> 2 2 2
<tr><td>0 1 1 2<td> 2 2 3
<tr><td>0 1 1 3<td> 2 2 4
<tr><td>0 1 2 2<td> 2 3 3
<tr><td>0 1 2 3<td> 2 3 4
<tr><td>0 1 3 3<td> 2 4 4
<tr><td>0 2 2 2<td> 3 3 3
<tr><td>0 2 2 3<td> 3 3 4
<tr><td>0 2 3 3<td> 3 4 4
<tr><td>0 3 3 3<td> 4 4 4
</table>
<p>Now we can observe that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cbinom74%3d%5cbinom73%24"> (they are both 35) so that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%285%2c3%29%20%3d%0aC%284%2c4%29%24">. We might ask if there is a combinatorial proof of this
fact, consisting of a natural bijection between <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c3%29%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c4%29%24">.
Using the relation <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28%5cspadesuit%29%24"> we have:</p>
<p>$$
\begin{eqnarray}
C(4,4) & = & C(3, 4) + & C(4,3) \\
C(5,3) & = & & C(4,3) + C(5,2) \\
\end{eqnarray}$$</p>
<p>so part of the bijection, at least, is clear:
There are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%284%2c3%29%24"> elements of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c4%29%24">
that begin with a zero,
and also <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%284%2c3%29%24"> elements of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c%203%29%24"> that do <em>not</em> begin with a zero, so
whatever the bijection is, it ought to match up these two subsets of
size 20. This is perfectly straightforward; simply match up <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%c2%ab0%2c%0aa%2c%20b%2c%20c%c2%bb%24"> (blue) with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%c2%aba%2b1%2c%20b%2b1%2c%20c%2b1%c2%bb%24"> (pink), as shown at
right.</p>
<p>But finding the other half of the bijection, between <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%283%2c4%29%24"> and
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c2%29%24">, is not so straightforward. (Both have 15 elements, but we
are looking for not just any bijection but for one that respects the
structure of the elements.) We could apply the recurrence
again, to obtain:</p>
<p>$$
\begin{eqnarray}
C(3,4) & = \color{darkred}{C(2, 4)} + \color{darkblue}{C(3,3)} \\
C(5,2) & = \color{darkblue}{C(4,2)} + \color{darkred}{C(5,1)}
\end{eqnarray}$$</p>
<p>and since $$
\begin{eqnarray}
\color{darkred}{C(2, 4)} & = \color{darkred}{C(5,1)} \\
\color{darkblue}{C(3,3)} & = \color{darkblue}{C(4,2)}
\end{eqnarray}$$</p>
<p>we might expect the bijection to continue in that way, mapping
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccolor%7bdarkred%7d%7bS%282%2c4%29%20%5cleftrightarrow%20S%285%2c1%29%7d%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccolor%7bdarkblue%7d%7bS%283%2c3%29%20%5cleftrightarrow%0aS%284%2c2%29%7d%24">. Indeed there is such a bijection, and it is very nice.</p>
<p>To find the bijection we will take a detour through bitstrings. There
is a natural bijection between <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2c%20n%29%24"> and the bit strings that
contain <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%2d1%24"> zeroes and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%24"> ones. Rather than explain it with
pseudocode, I will give some examples, which I think will make the
point clear. Consider the sequence <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%c2%ab1%2c%201%2c%203%2c%204%c2%bb%24">. Suppose you are trying to communicate this sequence to a
computer. It will ask you the following questions, and you should
give the corresponding answers:</p>
<ul>
<li>“Is the first symbol 0?” (“No”)</li>
<li>“Is the first symbol 1?” (“Yes”)</li>
<li>“Is the second symbol 1?” (“Yes”)</li>
<li>“Is the third symbol 1?” (“No”)</li>
<li>“Is the third symbol 2?” (“No”)</li>
<li>“Is the third symbol 3?” (“Yes”)</li>
<li>“Is the fourth symbol 3?” (“No”)</li>
<li>“Is the fourth symbol 4?” (“Yes”)</li>
</ul>
<p>At each stage the
computer asks about the identity of the next symbol. If the answer is
“yes” the computer has learned another symbol and moves on to the next
element of the sequence. If it is “no” the computer tries guessing a
different symbol. The “yes” answers become ones and “no”
answers become zeroes, so that the resulting bit string is <code>0 1 1 0 0 1 0 1</code>. </p>
<p>It sometimes happens that the computer figures out all the elements of
the sequence before using up its <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%2bd%2d1%24"> questions; in this case we
pad out the bit string with zeroes, or we
can imagine that the computer asks some pointless questions to which the answer
is “no”. For example, suppose the sequence is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%c2%ab0%2c%201%2c%201%2c%201%c2%bb%24">:</p>
<ul>
<li>“Is the first symbol 0?” (“Yes”)</li>
<li>“Is the second symbol 0?” (“No”)</li>
<li>“Is the second symbol 1?” (“Yes”)</li>
<li>“Is the third symbol 1?” (“Yes”)</li>
<li>“Is the fourth symbol 1?” (“Yes”)</li>
</ul>
<p>The bit string is <code>1 0 1 1 1 0 0 0</code>, where the final three <code>0</code> bits are
the padding.</p>
<p>We can reverse the process, simply taking over the role of the
computer. To find the sequence that corresponds to the bit string
<code>0 1 1 0 1 0 0 1</code>, we ask the questions ourselves and use the bits as the
answers:</p>
<ul>
<li>“Is the first symbol 0?” (“No”)</li>
<li>“Is the first symbol 1?” (“Yes”)</li>
<li>“Is the second symbol 1?” (“Yes”)</li>
<li>“Is the third symbol 1?” (“No”)</li>
<li>“Is the third symbol 2?” (“Yes”)</li>
<li>“Is the fourth symbol 2?” (“No”)</li>
<li>“Is the fourth symbol 3?” (“No”)</li>
<li>“Is the fourth symbol 4?” (“Yes”)</li>
</ul>
<p>We have recovered the sequence <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%c2%ab1%2c%201%2c%202%2c%204%c2%bb%24"> from the
bit string <code>0 1 1 0 1 0 0 1</code>.</p>
<p>This correspondence establishes relation <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28%5cheartsuit%29%24"> in a
different way from before: since there is a natural bijection between
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2c%20n%29%24"> and the bit strings with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%2d1%24"> zeroes and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%24"> ones,
there are certainly <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cbinom%7bn%2bd%2d1%7d%7bd%2d1%7d%24"> of them as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28%5cheartsuit%29%24">
says because there are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24n%2bd%2d1%24"> bits and we may choose any <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24d%2d1%24"> to
be the zeroes.</p>
<p>We wanted to see why <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%285%2c3%29%20%3d%20C%284%2c4%29%24">. The detour above shows that
there is a simple bijection between</p>
<blockquote>
<p><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c3%29%24"> and the bit strings with 4 zeroes and 3 ones</p>
</blockquote>
<p>on one hand, and between</p>
<blockquote>
<p><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c4%29%24"> and the bit strings with 3 zeroes and 4 ones</p>
</blockquote>
<p>on the other hand. And of course
the bijection between the two sets of <em>bit strings</em> is completely
obvious: just exchange the zeroes and the ones.</p>
<p>The table below shows the complete bijection between <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c4%29%24"> and
its descriptive bit strings (on the left in blue) and between <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c%0a3%29%24"> and its descriptive bit strings (on the right in pink) and that
the two sets of bit strings are complementary. Furthermore the top
portion of the table shows that the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c3%29%24"> subsets of the two
families correspond, as they should—although the correct
correspondence is the <em>reverse</em> of the one that was displayed earlier
in the article,
not the suggested
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%c2%ab0%2c%20a%2c%20b%2c%20c%c2%bb%20%5cleftrightarrow%20%c2%aba%2b1%2c%20b%2b1%2c%20c%2b1%c2%bb%24"> at all.
Instead, in the correct table, the initial digit
of the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c4%29%24"> entry says how many zeroes appear in the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c3%29%24">
entry, and vice versa; then the increment to the next digit says how many ones, and so forth.</p>
<p><style>
table .tb { padding-color: purple; }
table .tb td { text-align: center; padding: 1px 4px }
td .c2 { border-right: dotted thin black }
th .c2 { border-right: dotted thin black }
</style></p>
<p align="center"><table class="tb" style="border-collapse: collapse; border: solid black 2px;">
<colgroup>
<col style="background: lightblue; border-right">
<col style="background: rgb(193,236,250);">
</colgroup>
<colgroup>
<col style="background: rgb(255,212,223);">
<col style="background: pink">
</colgroup>
<tbody>
<tr style="border-bottom: solid black 1px"><th><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%284%2c4%29%24"><th class="c2">(bits)<th>(complement bits)<th><img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c3%29%24">
<tr><td class="c1"> 0 0 0 0 <td class="c2"> 1 1 1 1 0 0 0 <td class="c3"> 0 0 0 0 1 1 1 <td class="c4"> 4 4 4
<tr><td class="c1"> 0 0 0 1 <td class="c2"> 1 1 1 0 1 0 0 <td class="c3"> 0 0 0 1 0 1 1 <td class="c4"> 3 4 4
<tr><td class="c1"> 0 0 0 2 <td class="c2"> 1 1 1 0 0 1 0 <td class="c3"> 0 0 0 1 1 0 1 <td class="c4"> 3 3 4
<tr><td class="c1"> 0 0 0 3 <td class="c2"> 1 1 1 0 0 0 1 <td class="c3"> 0 0 0 1 1 1 0 <td class="c4"> 3 3 3
<tr><td class="c1"> 0 0 1 1 <td class="c2"> 1 1 0 1 1 0 0 <td class="c3"> 0 0 1 0 0 1 1 <td class="c4"> 2 4 4
<tr><td class="c1"> 0 0 1 2 <td class="c2"> 1 1 0 1 0 1 0 <td class="c3"> 0 0 1 0 1 0 1 <td class="c4"> 2 3 4
<tr><td class="c1"> 0 0 1 3 <td class="c2"> 1 1 0 1 0 0 1 <td class="c3"> 0 0 1 0 1 1 0 <td class="c4"> 2 3 3
<tr><td class="c1"> 0 0 2 2 <td class="c2"> 1 1 0 0 1 1 0 <td class="c3"> 0 0 1 1 0 0 1 <td class="c4"> 2 2 4
<tr><td class="c1"> 0 0 2 3 <td class="c2"> 1 1 0 0 1 0 1 <td class="c3"> 0 0 1 1 0 1 0 <td class="c4"> 2 2 3
<tr><td class="c1"> 0 0 3 3 <td class="c2"> 1 1 0 0 0 1 1 <td class="c3"> 0 0 1 1 1 0 0 <td class="c4"> 2 2 2
<tr><td class="c1"> 0 1 1 1 <td class="c2"> 1 0 1 1 1 0 0 <td class="c3"> 0 1 0 0 0 1 1 <td class="c4"> 1 4 4
<tr><td class="c1"> 0 1 1 2 <td class="c2"> 1 0 1 1 0 1 0 <td class="c3"> 0 1 0 0 1 0 1 <td class="c4"> 1 3 4
<tr><td class="c1"> 0 1 1 3 <td class="c2"> 1 0 1 1 0 0 1 <td class="c3"> 0 1 0 0 1 1 0 <td class="c4"> 1 3 3
<tr><td class="c1"> 0 1 2 2 <td class="c2"> 1 0 1 0 1 1 0 <td class="c3"> 0 1 0 1 0 0 1 <td class="c4"> 1 2 4
<tr><td class="c1"> 0 1 2 3 <td class="c2"> 1 0 1 0 1 0 1 <td class="c3"> 0 1 0 1 0 1 0 <td class="c4"> 1 2 3
<tr><td class="c1"> 0 1 3 3 <td class="c2"> 1 0 1 0 0 1 1 <td class="c3"> 0 1 0 1 1 0 0 <td class="c4"> 1 2 2
<tr><td class="c1"> 0 2 2 2 <td class="c2"> 1 0 0 1 1 1 0 <td class="c3"> 0 1 1 0 0 0 1 <td class="c4"> 1 1 4
<tr><td class="c1"> 0 2 2 3 <td class="c2"> 1 0 0 1 1 0 1 <td class="c3"> 0 1 1 0 0 1 0 <td class="c4"> 1 1 3
<tr><td class="c1"> 0 2 3 3 <td class="c2"> 1 0 0 1 0 1 1 <td class="c3"> 0 1 1 0 1 0 0 <td class="c4"> 1 1 2
<tr style="border-bottom: solid black 1px"><td class="c1"> 0 3 3 3 <td class="c2"> 1 0 0 0 1 1 1 <td class="c3"> 0 1 1 1 0 0 0 <td class="c4"> 1 1 1
<tr><td class="c1"> 1 1 1 1 <td class="c2"> 0 1 1 1 1 0 0 <td class="c3"> 1 0 0 0 0 1 1 <td class="c4"> 0 4 4
<tr><td class="c1"> 1 1 1 2 <td class="c2"> 0 1 1 1 0 1 0 <td class="c3"> 1 0 0 0 1 0 1 <td class="c4"> 0 3 4
<tr><td class="c1"> 1 1 1 3 <td class="c2"> 0 1 1 1 0 0 1 <td class="c3"> 1 0 0 0 1 1 0 <td class="c4"> 0 3 3
<tr><td class="c1"> 1 1 2 2 <td class="c2"> 0 1 1 0 1 1 0 <td class="c3"> 1 0 0 1 0 0 1 <td class="c4"> 0 2 4
<tr><td class="c1"> 1 1 2 3 <td class="c2"> 0 1 1 0 1 0 1 <td class="c3"> 1 0 0 1 0 1 0 <td class="c4"> 0 2 3
<tr><td class="c1"> 1 1 3 3 <td class="c2"> 0 1 1 0 0 1 1 <td class="c3"> 1 0 0 1 1 0 0 <td class="c4"> 0 2 2
<tr><td class="c1"> 1 2 2 2 <td class="c2"> 0 1 0 1 1 1 0 <td class="c3"> 1 0 1 0 0 0 1 <td class="c4"> 0 1 4
<tr><td class="c1"> 1 2 2 3 <td class="c2"> 0 1 0 1 1 0 1 <td class="c3"> 1 0 1 0 0 1 0 <td class="c4"> 0 1 3
<tr><td class="c1"> 1 2 3 3 <td class="c2"> 0 1 0 1 0 1 1 <td class="c3"> 1 0 1 0 1 0 0 <td class="c4"> 0 1 2
<tr><td class="c1"> 1 3 3 3 <td class="c2"> 0 1 0 0 1 1 1 <td class="c3"> 1 0 1 1 0 0 0 <td class="c4"> 0 1 1
<tr><td class="c1"> 2 2 2 2 <td class="c2"> 0 0 1 1 1 1 0 <td class="c3"> 1 1 0 0 0 0 1 <td class="c4"> 0 0 4
<tr><td class="c1"> 2 2 2 3 <td class="c2"> 0 0 1 1 1 0 1 <td class="c3"> 1 1 0 0 0 1 0 <td class="c4"> 0 0 3
<tr><td class="c1"> 2 2 3 3 <td class="c2"> 0 0 1 1 0 1 1 <td class="c3"> 1 1 0 0 1 0 0 <td class="c4"> 0 0 2
<tr><td class="c1"> 2 3 3 3 <td class="c2"> 0 0 1 0 1 1 1 <td class="c3"> 1 1 0 1 0 0 0 <td class="c4"> 0 0 1
<tr><td class="c1"> 3 3 3 3 <td class="c2"> 0 0 0 1 1 1 1 <td class="c3"> 1 1 1 0 0 0 0 <td class="c4"> 0 0 0
</tbody></table></p>
<p>Observe that since <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2cn%29%20%3d%20%5cbinom%7bn%2bd%2d1%7d%7bd%2d1%7d%20%3d%20%5cbinom%7bn%2bd%2d1%7d%7bn%7d%20%3d%0aC%28n%2b1%2c%20d%2d1%29%24"> we have in general that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2cn%29%20%3d%20C%28n%2b1%2c%20d%2d1%29%24">, which
may be surprising. One might have guessed that since <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%285%2c3%29%20%3d%0aC%284%2c4%29%24">, the relation was <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24C%28d%2cn%29%20%3d%20C%28d%2b1%2c%20n%2d1%29%24"> and that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2cn%29%24"> would have the same structure as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%28d%2b1%2c%20n%2d1%29%24">, but it isn't so.
The two arguments exchange roles. Following the same path, we can
identify many similar ‘coincidences’. For example, there is a simple
bijection between the original set of 715 puzzles, which was
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%2810%2c4%29%24">, and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%285%2c9%29%24">, the set of nondecreasing sequences of
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%240%5cldots%204%24"> of length <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%249%24">.</p>
<p>[ Thanks to Bence Kodaj for a correction. ]</p>
<p>[ Addendum 20170829: Conway and Guy, in <em>The Book of Numbers</em>, describe the same bijection, but a little differently; see their discussion of the <em>Sweet Seventeen</em> deck on pages 70–71. ]</p>
Let's decipher a thousand-year-old magic square
https://blog.plover.com/2016/12/15#magic-square-puzzle
<p>The <a href="https://en.wikipedia.org/wiki/Parshvanatha_temple,_Khajuraho">Parshvanatha
temple</a>
in Madhya Pradesh, India was built around 1,050 years ago. Carved at
its entrance is this magic square:</p>
<p align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/magic-square.png"></p>
<p>The digit signs have changed in the past thousand years, but it's a
quick and fun puzzle to figure out what they mean using only the
information that this is, in fact, a magic square.</p>
<p>A solution follows. No peeking until you've tried it yourself!</p>
<hr />
<p>There are 9 one-digit entries
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/1.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/2.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/3.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/z.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/E.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/><br>
and 7 two-digit entries
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/12.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/18.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/13.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/11.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/1E.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/10.png"/>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/14.png"/><br>
so we can guess
that the entries are the numbers 1 through 16, as is usual, and the
magic sum is 34. The <img src="https://pic.blog.plover.com/math/magic-square-puzzle/1.png"/> appears in the
same position in all the two-digit numbers, so it's the digit 1. The
other digit of the numeral <img src="https://pic.blog.plover.com/math/magic-square-puzzle/10.png"/> is <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/0.png"/>, and this must be zero. If it were
otherwise, it would appear on its own, as does for example the
<img
src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/> from <img src="https://pic.blog.plover.com/math/magic-square-puzzle/18.png"/> or the
<img
src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> from <img src="https://pic.blog.plover.com/math/magic-square-puzzle/14.png"/>.</p>
<p>It is tempting to imagine that <img src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> is 4.
But we can see it's not so. Adding up the rightmost column, we get</p>
<p align=center>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/18.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/11.png"/> + <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/> = <br>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/18.png"/> + 11 + <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/> = <br>
(10 + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/>) + 11 + <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/>
= 34,
</p>
<p>so that <img src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> must be an <em>odd</em> number. We
know it isn't 1 (because <img src="https://pic.blog.plover.com/math/magic-square-puzzle/1.png"/> is 1), and it
can't be 7 or 9 because <img src="https://pic.blog.plover.com/math/magic-square-puzzle/14.png"/> appears in the
bottom row and there is no 17 or 19. So <img src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/>
must be 3 or 5.</p>
<p>Now if <img src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> were 3, then <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/14.png"/> would be 13, and the third column would be</p>
<p align=center>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/1.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/10.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/14.png"/> = <br>
1 + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/> + 10 + 13 = 34,
</p>
<p>and then <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/> would be 10, which is too big. So <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/> must be 5, and this means that <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/> is 4 and <img src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/> is 8.
(<img src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/> appears only a as a single-digit
numeral, which is consistent with it being 8.)</p>
<p>The top row has</p>
<p align=center>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/12.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/1.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/18.png"/> = <br>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/12.png"/> + 1 + 14 = <br>
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/> + (10 + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/2.png"/>) + 1 + 14 = 34</p>
<p>so that <img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/> + <img src="https://pic.blog.plover.com/math/magic-square-puzzle/2.png"/>
= 9. <img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/> only appears as a single digit and
we already used 8 so <img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/> must be 7 or 9.
But 9 is too big, so it must be 7, and then <img
src="https://pic.blog.plover.com/math/magic-square-puzzle/2.png"/> is 2.</p>
<p><img src="https://pic.blog.plover.com/math/magic-square-puzzle/z.png"/> is the only remaining unknown single-digit
numeral, and we already know 7 and 8, so
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/z.png"/> is 9. The leftmost column tells us
that <img src="https://pic.blog.plover.com/math/magic-square-puzzle/1E.png"/> is 16, and the last two entries,
<img src="https://pic.blog.plover.com/math/magic-square-puzzle/13.png"/> and <img src="https://pic.blog.plover.com/math/magic-square-puzzle/3.png"/> are
easily discovered to be 13 and 3. The decoded square is:</p>
<p align=center>
<table cellspacing=20>
<tr><td><table>
<tr><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/9.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/12.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/1.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/18.png"/>
<tr><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/2.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/13.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/7.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/11.png"/>
<tr><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/1E.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/3.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/10.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/4.png"/>
<tr><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/z.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/E.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/14.png"/><td align=center><img src="https://pic.blog.plover.com/math/magic-square-puzzle/8.png"/>
</table>
<td>
<table>
<tr><td align=center>7<td align=center>12<td align=center>1<td align=center>14
<tr><td align=center>2<td align=center>13<td align=center>8<td align=center>11
<tr><td align=center>16<td align=center>3<td align=center>10<td align=center>5
<tr><td align=center>9<td align=center>6<td align=center>15<td align=center>4
</table>
</table></p>
<p>I like that people look at the right-hand column and immediately see 18 + 11 + 4 + 8 but it's actually
14 + 11 + 5 + 4.</p>
<p>This is <a href="https://en.wikipedia.org/wiki/Most-perfect_magic_square">an extra-special magic
square</a>: not
only do the ten rows, columns, and diagonals all add up to 34, so do
all the four-cell subsquares, so do any four squares arranged
symmetrically about the center, and so do all the broken diagonals
that you get by wrapping around at the edges.</p>
<p>[ Addendum: It has come to my attention that the digit symbols in
the magic square are not too different from the current forms
of
<a href="https://en.wikipedia.org/wiki/Hindu%E2%80%93Arabic_numeral_system#Symbols">the digit symbols in the Gujarati script</a>. ]</p>
<p>[ Addendum 20161217: The temple is not very close to Gujarat or to
the area in which Gujarati is common, so I guess that the digit
symbols in Indian languages have evolved in the past thousand
years, with the Gujarati versions remaining closest to the
ancient forms, or else perhaps Gujarati was spoken more widely a
thousand years ago. I would be interested to hear about this
from someone who knows. ]</p>
<p>[ Addendum 20170130: <a href="https://shreevatsa.wordpress.com/">Shreevatsa R.</a> has contributed <a href="https://blog.plover.com/lang/magic-square-puzzle-2.html">a detailed discussion of the history of the digit symbols</a>. ]</p>
Decomposing a function into its even and odd parts
https://blog.plover.com/2016/07/30#even-odd
<p>As I have mentioned before, I am not a sudden-flash-of-insight
person. Every once in a while it happens, but usually my thinking style is to
minutely examine a large mass of examples and then gradually
synthesize some conclusion about them. I am a penetrating but slow
thinker. But there have been a few occasions in my life when the
solution to a problem struck me suddenly out of the blue.</p>
<p>One such occasion was on the first day of my sophomore honors physics
class in 1987. This was one of the best classes I took in my college
career. It was given by Professor Stephen Nettel, and it was about
resonance phenomena. I love when a course has a single overarching
theme and proceeds to examine it in detail; that is all too rare. I
deeply regret leaving my copy of the course notes in a restaurant in
1995.</p>
<p>The course was very difficult, But also very satisfying. It was also
somewhat hair-raising, because of Professor Nettel's habit of saying,
all through the second half “Don't worry if it doesn't seem to make
any sense, it will all come together for you during the final exam.”
This was not reassuring. But he was right! It <em>did</em> all come
together during the final exam.</p>
<p>The exam had two sets of problems. The problems on the left side of
the exam paper concerned some mechanical system, I think a rod fixed
at one end and free at the other, or something like that. This set of
problems asked us to calculate the resonant frequency of the rod, its
rate of damping at various driving frequencies, and related matters.
The right-hand problems were about an electrical system involving a
resistor, capacitor, and inductor. The questions were the same, and
the answers were formally identical, differing only in the details: on
the left, the answers involved length, mass and stiffness of the rod,
and on the right, the resistance, capacitance, and inductance of the
electrical components. It was a brilliant exam, and I have never
learned so much about a subject <em>during</em> the final exam.</p>
<p>Anyway, I digress. After the first class, we were assigned homework.
One of the problems was</p>
<blockquote>
<p>Show that every function is the sum of an even function and an odd
function.</p>
</blockquote>
<p>(Maybe I should explain that an even function is one which is
symmetric across the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24y%24">-axis; formally it is a function <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%24"> for
which <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%28x%29%20%3d%20f%28%2dx%29%24"> for every <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">. For example, the function
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%5e2%2d4%24">, shown below left. An odd function is one which is
symmetric under a half-turn about the origin; formally it satisfies
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%28x%29%20%3d%20%2df%28%2dx%29%24"> for all <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">. For example <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7bx%5e3%7d%7b20%7d%24">, shown
below right.)</p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/even.png">
<img src="https://pic.blog.plover.com/math/even-odd/odd.png">
</p>
<p>I found this claim very surprising, and we had no idea how to solve
it. Well, not quite <em>no</em> idea: I knew that functions could be expanded in
<a href="http://enwp.org/fourier_series">Fourier series</a>, as the sum of a sine
series and a cosine series, and the sine part was odd while the cosine
part was even. But this seemed like a bigger hammer than was
required, particularly since new sophomores were not expected to know
about Fourier series.</p>
<p>I had the privilege to be in that class with
<a href="https://en.wikipedia.org/wiki/Ron_Buckmire">Ron Buckmire</a>, and I
remember we stood outside the class building in the autumn sunshine
and discussed the problem. I might have been thinking that perhaps
there was some way to replace the negative part of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%24"> with a
reflected copy of the positive part to make an even function, and
maybe that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%28x%29%20%2b%20f%28%2dx%29%24"> was always even, when I was hit from the
blue with the solution:</p>
<p>$$
\begin{align}
f_e(x) & = \frac{f(x) + f(-x)}2 \text{ is even},\\
f_o(x) & = \frac{f(x) - f(-x)}2 \text{ is odd, and}\\
f(x) &= f_e(x) + f_o(x)
\end{align}
$$</p>
<p>So that was that problem solved. I don't remember the other three
problems in that day's homework, but I have remembered that one ever
since.</p>
<p>But for some reason, it didn't occur to me until today to think about
what those functions actually looked like. Of course, if <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%24">
itself is even, then <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_e%20%3d%20f%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_o%20%3d%200%24">, and similarly if
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%24"> is odd. But most functions are neither even nor odd.</p>
<p>For example, consider the function <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%5ex%24">, which is neither even nor
odd. Then we get </p>
<p>$$
\begin{align}
f_e(x) & = \frac{2^x + 2^{-x}}2\\
f_o(x) & = \frac{2^x - 2^{-x}}2
\end{align}
$$</p>
<p>The graph is below left. The solid red line is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%5ex%24">, and the blue
and purple dotted lines are <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_e%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_o%24">. The red line is
the sum of the blue and purple lines. I thought this was very
interesting-looking, but a little later I realized that I had already known
what these graphs would look like, because <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%5ex%24"> is just like
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24e%5ex%24">, and for <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24e%5ex%24"> the even and odd components are exactly the
familiar <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccosh%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csinh%24"> functions. (Below left, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%242%5ex%24">; below right,
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24e%5ex%24">.)</p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/exp2.png">
<img src="https://pic.blog.plover.com/math/even-odd/exp.png">
</p>
<p>I wasn't expecting polynomials to be more interesting, but they were.
(Polynomials whose terms are all odd powers of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">, such as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%5e%7b13%7d%20%2d%0a4x%5e5%20%2b%20x%24">, are always odd functions,
and similarly polynomials whose terms are all even powers of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24"> are
even functions.) For example, consider <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28x%2d1%29%5e2%24">, which is neither
even nor odd. We don't even need the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_e%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_o%24"> formulas
to separate this into even and odd parts: just expand <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28x%2d1%29%5e2%24"> as
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%5e2%20%2d%202x%20%2b%201%24"> and separate it into odd and even powers, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%2d2x%24"> and
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%5e2%20%2b%201%24">:</p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/poly1.png">
</p>
<p>Or we could do <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7b%28x%2d1%29%5e3%7d3%24"> similarly, expanding it as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7bx%5e3%7d3%20%2d%20x%5e2%20%2b%0ax%20%2d%5cfrac13%24"> and separating this into <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%2dx%5e2%20%2d%5cfrac13%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7bx%5e3%7d3%20%2b%20x%24">:</p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/poly2.png">
</p>
<p>I love looking at these and seeing how the even blue line and the odd
purple line conspire together to make whatever red line I want.</p>
<p>I kept wanting to try familiar simple functions, like <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac1x%24">, but
many of these are either even or odd, and so are uninteresting for
this application. But you can make an even or an odd function into a
neither-even-nor-odd function just by translating it horizontally,
which you do by replacing <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24"> with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%2dc%24">. So the next function I
tried was <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac1%7bx%2b1%7d%24">, which is the translation of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%0a1x%24">. Here I got a surprise. I knew that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac1%7bx%2b1%7d%24"> was
undefined at <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3d%2d1%24">, so I graphed it only for <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3e%2d1%24">. But the
even component is <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac12%5cleft%28%5cfrac1%7b1%2bx%7d%2b%5cfrac1%7b1%2dx%7d%5cright%29%24">,
which is undefined at both <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3d%2d1%24"> and at <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3d%2b1%24">. Similarly the odd
component is undefined at two points. So the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%20%3d%20f%5c_o%20%2b%20f%5c_e%24">
formula does not work quite correctly, failing to produce the correct
value at <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3d1%24">, even though <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%24"> is defined there. In general, if
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%24"> is undefined at some <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3dc%24">, then the decomposition into even
and odd components fails at <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3d%2dc%24"> as well. The limit $$\lim_{x\to
-c} f(x) = \lim_{x\to -c} \left(f_o(x) + f_e(x)\right)$$ does hold, however. The
graph below shows the decomposition of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac1%7bx%2b1%7d%24">. </p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/hyper1.png">
</p>
<p>Vertical translations
are uninteresting: they leave <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_o%24"> unchanged and
translate <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24f%5c_e%24"> by the same amount, as you can verify algebraically
or just by thinking about it.</p>
<p>Following the same strategy I tried a cosine wave. The evenness of
the cosine function is one of its principal properties, so I
translated it and used <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccos%20%28x%2b1%29%24">. The graph below is actually
for <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%245%5ccos%28x%2b1%29%24"> to prevent the details from being too compressed:</p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/cosine.png">
</p>
<p>This reminded me of the time I was fourteen and graphed <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csin%20x%20%2b%0a%5ccos%20x%24"> and was surprised to see that it was another perfect
sinusoid. But I realized that there was a simple way to understand
this. I already knew that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccos%28x%20%2b%20y%29%20%3d%20%5csin%20x%5ccos%20y%20%2b%20%5csin%20y%20%5ccos%0ax%24">. If you take <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24y%3d%5cfrac%5cpi4%24"> and multiply the whole thing by
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csqrt%202%24">, you get $$\sqrt2\cos\left(x + \frac\pi4\right) =
\sqrt2\sin x\cos\frac\pi4 + \sqrt2\cos x\sin\frac\pi4 = \sin x + \cos
x$$ so that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csin%20x%20%2b%20%5ccos%20x%24"> is just a shifted, scaled cosine
curve. The decomposition of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccos%28x%2b1%29%24"> is even simpler because you
can work forward instead of backward and find that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccos%28x%2b1%29%20%3d%20%5csin%0ax%5ccos%201%20%2b%20%5ccos%20x%20%5csin%201%24">, and the first term is odd while the second
term is even, so that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5ccos%28x%2b1%29%24"> decomposes as a sum of an even and
an odd sinusoid as you see in the graph above.</p>
<p>Finally, I tried a
<a href="http://enwp.org/Poisson_distribution">Poisson distribution</a>, which is
highly asymmetric. The formula for the Poisson distribution is
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7b%5clambda%5exe%5e%5clambda%7d%7bx%21%7d%24">, for some constant <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5clambda%24">. The
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%21%20%24"> in the denominator is only defined for non-negative integer
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">, but you can extend it to fractional and negative <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24"> in the
usual way by using <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cGamma%28x%2b1%29%24"> instead, where <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cGamma%24"> is the
<a href="http://enwp.org/Gamma_function">Gamma function</a>. The <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cGamma%24">
function is undefined at zero and negative integers, but fortunately
what we need here is the
<a href="http://enwp.org/Gamma_function">reciprocal gamma function</a>
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac1%7b%5cGamma%28x%29%7d%24">, which is perfectly well-behaved. The results
are spectacular. The graph below has <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5clambda%20%3d%200%2e8%24">.</p>
<p align="center">
<img src="https://pic.blog.plover.com/math/even-odd/poisson.png">
</p>
<p>The part of this with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%5cge%200%24"> is the most interesting to me,
because the Poisson distribution has a very distinctive shape, and
once again I like seeing the blue and purple <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cGamma%24"> functions
working together to make it. I think it's just great how the red line
goes gently to zero as <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24"> increases, even though the even and the
odd components are going wild. (<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%21%20%24"> increases rapidly with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">,
so the reciprocal <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cGamma%24"> function goes rapidly to zero. But the
even and odd components also have a <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac1%7b%5cGamma%28%2dx%29%7d%24"> part, and
this is what dominates the blue and purple lines when <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%20%3e4%24">.)</p>
<p>On the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%5clt%200%24"> side it has no meaning for me, and it's just wiggly
lines. It hadn't occurred to me before that you could extend the
Poisson distribution function to negative <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">, and I still can't
imagine what it could mean, but I suppose why not. Probably some
statistician could explain to me what the Poisson distribution is
about when <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3c0%24">.</p>
<p>You can also consider the function <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csqrt%20x%24">, which breaks down
completely, because either <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csqrt%20x%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csqrt%7b%2dx%7d%24"> is undefined
except when <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%3d0%24">. So the claim that <em>every</em> function is the sum of
an even and an odd function fails here too. Except perhaps not! You
could probably consider the extension of the square root function to
the complex plane, and take one of its branches, and I suppose it
works out just fine. The geometric interpretation of evenness and
oddness are very different, of course, and you can't really draw the
graphs unless you have four-dimensional vision.</p>
<p>I have no particular point to make, except maybe that math is fun,
even elementary math (or perhaps especially elementary math) and it's
fun to see how it works out.</p>
<p>The beautiful graphs in this article were made with
<a href="https://www.desmos.com/">Desmos</a>. I had dreaded having to illustrate
my article with graphs from Gnuplot (ugh) or Wolfram|α (double
ugh) and was thrilled to find such a handsome alternative.</p>
<p>[ Addendum: I've just discovered that in Desmos you can include a parameter in the functions that it graphs, and attach the parameter to a slider. So for example you can arrange to have it display <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%28x%2bk%29%5e3%24"> or <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24e%5e%7b%2d%28x%2bk%29%5e2%7d%24">, with the value of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24k%24"> controlled by the slider, and have the graph move left and right on the plane as you adjust the slider, with its even and odd parts changing in real time to match. ]</p>
<p>[ For example, check out <a href="https://www.desmos.com/calculator/qc2tbh0xnv">travelling Gaussians</a> or <a href="https://www.desmos.com/calculator/mvptcfvx7f">varying sinusoid</a>. ]</p>
A simple but difficult arithmetic puzzle
https://blog.plover.com/2016/07/12#17-puzzle
<p>Lately my kids have been interested in puzzles of this type: You are
given a sequence of four digits, say 1,2,3,4, and your job is to
combine them with ordinary arithmetic operations (+, -, ×, and ÷) in any order to
make a target number, typically 24. For example, with 1,2,3,4, you
can go with $$((1+2)+3)×4 = 24$$ or with $$4×((2×3)×1) = 24.$$</p>
<p>We were stumped trying to make 6,6,5,2 total 24, so I hacked up a
solver; then we felt a little foolish when we saw the solutions,
because it is not that hard. But in the course of testing the solver,
I found the most challenging puzzle of this type that I've ever seen.
It is:</p>
<blockquote>
<p>Given 6,6,5,2, make 17.</p>
</blockquote>
<p>There are no underhanded tricks. For example, you may not concatenate
2 and 5 to make 25; you may not say <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%246%c3%b76%3d1%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%245%2b2%3d7%24"> and
concatenate 1 and 7 to make <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%2417%24">; you may not interpret the 17 as a
base 12 numeral, etc.</p>
<p>I hope to write a longer article about solvers in the next week or so.</p>
<p>[ Addendum 20170305: The next week or so, ha ha.
<a href="https://blog.plover.com/math/24-puzzle.html">Anyway, here it is</a>. ]</p>
The sage and the seven horses
https://blog.plover.com/2016/04/20#horse-puzzle
<p>A classic puzzle of mathematics goes like this:</p>
<blockquote>
<p>A father dies and his will states that his elder daughter should
receive half his horses, the son should receive one-quarter of the
horses, and the younger daughter should receive one-eighth of the
horses. Unfortunately, there are seven horses. The siblings are
arguing about how to divide the seven horses when a passing sage hears
them. The siblings beg the sage for help. The sage donates his own
horse to the estate, which now has eight. It is now easy to portion
out the half, quarter, and eighth shares, and having done so, the
sage's horse is unaccounted for. The three heirs return the surplus
horse to the sage, who rides off, leaving the matter settled fairly.</p>
</blockquote>
<p>(The puzzle is, what just happened?)</p>
<p>It's not hard to come up with variations on this. For example,
picking three fractions at random, suppose the will says that the
eldest child receives half the horses, the middle child receives
one-fifth, and the youngest receives one-seventh. But the estate has
only 59 horses and an argument ensues. All that is required for
the sage to solve the problem is to lend the estate eleven horses.
There are now 70, and after taking out the three bequests, <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%2470%20%2d%2035%20%2d%0a14%20%2d%2010%20%3d%2011%24"> horses remain and the estate settles its debt to the
sage.</p>
<p>But here's a variation I've never seen before. This time there are 13
horses and the will says that the three children should receive shares
of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac12%2c%20%5cfrac13%2c%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac14%24">. respectively. Now the
problem seems impossible, because <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac12%20%2b%20%5cfrac13%20%2b%20%5cfrac14%20%5cgt%0a1%24">. But the sage is equal to the challenge! She leaps into the
saddle of one of the horses and rides out of sight before the
astonished heirs can react. After a day of searching the heirs write
off the lost horse and proceed with executing the will. There are now
only 12 horses, and the eldest takes half, or six, while the middle
sibling takes one-third, or 4. The youngest heir should get three,
but only two remain. She has just opened her mouth to complain at her
unfair treatment when the sage rides up from nowhere and hands her the
reins to her last horse.</p>
Math.SE report 2015-08
https://blog.plover.com/2015/12/18#2015-08
<p>I only posted three answers in August, but two of them were interesting.</p>
<ul>
<li><p>In <a href="http://math.stackexchange.com/a/1381699/25554">why this <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5csigma%5cpi%5csigma%5e%7b%2d1%7d%24"> keeps apearing in my group
theory book? (cycle
decomposition)</a> the
querent asked about the “conjugation” operation that keeps cropping
up in group theory. Why is it important? I sympathize with this;
it wasn't adequately explained when I took group theory, and I had
to figure it out a long time later. Unfortunately I don't think I
picked the right example to explain it, so I am going to try again
now.</p>
<p>Consider the eight symmetries of the square. They are of five types:</p>
<ol>
<li>Rotation clockwise or counterclockwise by 90°.</li>
<li>Rotation by 180°.</li>
<li>Horizontal or vertical reflection</li>
<li>Diagonal reflection</li>
<li>The trivial (identity) symmetry</li>
</ol>
<p>What is meant when I say that a horizontal and a vertical reflection
are of the same ‘type’? Informally, it is that the horizontal
reflection looks just like the vertical reflection, if you turn your
head ninety degrees. We can formalize this by observing that if we
rotate the square 90°, then give it a horizontal flip, then rotate it
back, the effect is exactly to give it a vertical flip. In notation,
we might represent the horizontal flip by <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24">, the vertical flip by
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24V%24">, the clockwise rotation by <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5crho%24">, and the counterclockwise
rotation by <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5crho%5e%7b%2d1%7d%24">; then we have </p>
<p>$$ \rho H \rho^{-1} = V$$</p>
<p>and similarly </p>
<p>$$ \rho V \rho^{-1} = H.$$</p>
<p>Vertical flips do not look like diagonal flips—the diagonal flip leaves two of the corners in the same place, and the vertical flip does not—and indeed there is
no analogous formula with <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24H%24"> replaced with one of the <em>diagonal</em>
flips. However, if <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24D%5c_1%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24D%5c_2%24"> are the two diagonal flips,
then we <em>do</em> have</p>
<p>$$ \rho D_1 \rho^{-1} = D_2.$$</p>
<p>In general, When <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24"> and <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%24"> are
two symmetries, and there is some symmetry <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24"> for which </p>
<p>$$xax^{-1} = b$$</p>
<p>we say that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24"> is <em>conjugate to</em> <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24b%24">.
One can show that
conjugacy is an equivalence relation, which means that the symmetries
of any object can be divided into separate “conjugacy classes” such that two
symmetries are conjugate if and only if they are in the same class.
For the square, the conjugacy classes are the five I listed earlier.</p>
<p>This conjugacy thing is important for telling when two symmetries
are group-theoretically “the same”, and have the same
group-theoretic properties. For example, the fact that the
horizontal and vertical flips move all four vertices, while the
diagonal flips do not. Another example is that a horizontal flip is
self-inverse (if you do it again, it cancels itself out), but a 90°
rotation is not (you have to do it <em>four</em> times before it cancels
out.) But the horizontal flip shares all its properties with the
vertical flip, because it is the same if you just turn your head.</p>
<p>Identifying this sameness makes certain kinds of arguments much
simpler. For example, in <a href="http://blog.plover.com/math/polya-burnside.html">counting
squares</a>, I wanted to
count the number of ways of coloring the faces of a cube, and instead
of dealing with the 24 symmetries of the cube, I only needed to deal
with their 5 conjugacy classes.</p>
<p>The example I gave in my math.se answer was maybe less perspicuous. I
considered the symmetries of a sphere, and talked about how two
rotations of the sphere by 17° are conjugate, regardless of what axis
one rotates around. I thought of the square at the end, and threw it
in, but I wish I had started with it.</p></li>
<li><p><a href="http://math.stackexchange.com/a/1404453/25554">How to convert a decimal to a fraction
easily?</a> was the
month's big winner. OP wanted to know how to take a decimal like
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%240%2e3760683761%24"> and discover that it can be written as
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7b44%7d%7b117%7d%24">. The right answer to this is of course to use
continued fraction theory, but I did not want to write a long
treatise on continued fractions, so I stripped down the theory to
obtain an algorithm that is slower, but much easier to understand.</p>
<p>The algorithm is just binary search, but with a twist. If you are looking for a
fraction for <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">, and you know <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%20ab%20%3c%20x%20%3c%20%5cfrac%20cd%24">, then
you construct the mediant <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7ba%2bc%7d%7bb%2bd%7d%24"> and compare it with
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">. This gives you a smaller interval in which to search for
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">, and the reason you use the mediant instead of using
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac12%5cleft%28%5cfrac%20ab%20%2b%20%5cfrac%20cd%5cright%29%24"> as usual is that if you use the
mediant you are guaranteed to exactly nail all the best rational
approximations of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24x%24">. This is the algorithm I
described a few years ago in <a href="https://blog.plover.com/math/age-fraction-2.html">your age as a fraction,
again</a>; there the binary search proceeds
down the branches of the Stern-Brocot tree to find a fraction close
to <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%240%2e368%24">.</p></li>
</ul>
<hr />
<p>I did ask a question this month: I was looking for a <a href="http://math.stackexchange.com/q/1405312/25554">simpler version
of the dogbone space
construction</a>. The
dogbone space is a very peculiar counterexample of general topology,
originally constructed by R.H. Bing. <a href="https://blog.plover.com/math/R3-root.html">I mentioned it here in
2007</a>, and said, at the time:</p>
<blockquote>
<p>[The paper] is on my desk, but I have not read this yet, and I may never.</p>
</blockquote>
<p>I did try to read it, but I did not try very hard, and I did not
understand it. So my question this month was if there was a simpler
example of the same type. I did not receive an answer, just a
followup comment that no, there is no such example.</p>
Math.SE report 2015-07
https://blog.plover.com/2015/08/16#2015-07
<p>My overall SE posting volume was down this month, and not only did I
post relatively few interesting items, I've already written <a href="https://blog.plover.com/math/ounce-of-theory-2.html">a whole
article about the most interesting
one</a>. So this will be a short
report.</p>
<ul>
<li><p>I already wrote up <a href="http://math.stackexchange.com/questions/1376640">Building a box from smaller
boxes</a> on the blog
<a href="https://blog.plover.com/math/ounce-of-theory-2.html">here</a>. But maybe I have a
couple of extra remarks. First, the other guy's proposed solution
is awful. It's long and complicated, which is forgivable if it had
answered the question, but it doesn't. And the key point is “blah
blah blah therefore code a solver which visits all configurations of
the search space”. Well heck, if this post had just been one
sentence that ended with “code a solver which visits all
configurations of the search space” I would not have any complaints
about that. </p>
<p>As an undergraduate I once gave a talk on this topic. One of my
examples was the problem of packing 31 dominoes into a chessboard
from which two squares have been deleted. There is a simple
combinatorial argument why this is impossible if the two deleted
squares are the same color, say if they are opposite corners: each
domino must cover one square of each color. But if you don't take
time to think about the combinatorial argument you could waste a lot
of time on computer search learning that there is no solution in
that case, and completely miss the deeper understanding that it
brings you. So this has been on my mind for a long time.</p></li>
<li><p>I wrote a few posts this month where I thought I gave good hints.
In <a href="http://math.stackexchange.com/questions/1371330/">How to scale an unit vector <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24u%24"> in such way that <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%20u%5ccdot%0a%20%20u%3d1%24"> where <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%24"> is a
scalar</a> I think I
did a good job identifying the original author's confusion; he was
conflating his original unit vector <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24u%24"> and the scaled, leading
him to write <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24au%5ccdot%20u%3d1%24">. This is sure to lead to confusion. So
I led him to the point of writing <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24a%28bv%29%5ccdot%28bv%29%3d1%24"> and let him
take it from there. The other proposed solution is much more rote
and mechanical. (“Divide this by that…”)</p>
<p>In <a href="ttp://math.stackexchange.com/questions/1362089/">Find numbers <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5coverline%7babcd%7d%24"> so that
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5coverline%7babcd%7d%2b%5coverline%7bbcd%7d%2b%5coverline%7bcd%7d%2bd%2b1%3d%5coverline%7bdcba%7d%24"></a>
the OP got stuck partway through and I specifically addressed the
stuckness; other people solved the problem from the beginning. I
think that's the way to go, if the original proposal was never going
to work, especially if you stop and say <em>why</em> it was never going to
work, but this time OP's original suggestion was perfectly good and
she just didn't know how to get to the next step. By the way, the
notation <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5coverline%7babcd%7d%24"> here means the number
<img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%241000a%2b100b%2b10c%2bd%24">.</p>
<p>In <a href="http://math.stackexchange.com/questions/1347223">Help finding the limit of this series <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24%5cfrac%7b1%7d%7b4%7d%20%2b%20%20%5cfrac%7b1%7d%7b8%7d%20%2b%20%5cfrac%7b1%7d%7b16%7d%20%2b%20%5cfrac%7b1%7d%7b32%7d%20%2b%20%20%5ccdots%24"></a> it would
have been really easy to say “use the formula” or to analyze the
series de novo, but I think I <em>almost</em> hit the nail on the head
here: it's just like <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%241%2b%5cfrac12%20%2b%20%5cfrac%7b1%7d%7b4%7d%20%2b%20%5cfrac%7b1%7d%7b8%7d%20%2b%0a%20%20%5cfrac%7b1%7d%7b16%7d%20%2b%20%5cfrac%7b1%7d%7b32%7d%20%2b%20%5ccdots%24">, which I bet OP already
knows, except a little different. But I pointed out the wrong
difference: I observed that the first sequence is one-fourth the
second one (which it is) but it would have been simpler to observe
that it's just the second one without the <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%241%2b%5cfrac12%24">. I had to
review it just now to give the simpler explanation, but I sure wish
I'd thought of it at the time. Nobody else pointed it out either.
Best of all, would have been to mention <em>both</em> methods. If you can
notice both of them you can solve the problem <em>without</em> the advance
knowledge of the value of <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%241%2b%5cfrac12%2b%5cfrac14%2b%5cldots%24">, because you
have <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%244S%20%3d%201%2b%5cfrac12%20%2b%20S%24"> and then solve for <img src="https://chart.apis.google.com/chart?chf=bg,s,00000000&cht=tx&chl=%24S%24">.</p>
<p>In <a href="http://math.stackexchange.com/questions/1358933/">Visualization of Rhombus made of Radii and
Chords</a> it seemed
that OP just needed to see a diagram (“I really really don't see how
two circles can form a rhombus?”), so I drew one. </p></li>
</ul>