The yellow row of the table above highlights an example of one of the days: the 21 gifts given on the sixth day: 6 geese a-laying, 5 golden rings, 4 calling birds, 3 french hens, 2 turtle doves, and a partridge in a pear tree.
The right-most column (green) is the sum of each day’s gift total using the approach described in part 1.
The bottom right-most cell (purple) shows the addition of the numbers in the green cells to give the total number of gifts for the 12 days of Christmas.
The final row (blue) shows that there are:
12 lots of gift 1 = 12
11 lots of gift 2 = 22
10 lots of gift 3 = 30
9 lots of gift 4 = 36
8 lots of gift 5 = 40
7 lots of gift 6 = 42
6 lots of gift 7 = 42
5 lots of gift 8 = 40
4 lots of gift 9 = 36
3 lots of gift 10 = 30
2 lots of gift 11 = 22
1 lot of gift 12 = 12
These values also add to 364.
This amounts to multiplying each value in the sequence 12 to 1 by those numbers in reverse order, 1 to 12, then adding the resulting products:
You may notice that the first six terms in the addition above repeat, but in reverse, after the first occurrence of 42 (the meaning of life!), so the calculation can be shortened to:
This seems like a nice simplification.
But as nice as this is, it doesn’t help us with generalising to any value of n, such as 100, since even if the summation sequence can be halved for any sequence, for large values of n, that’s still a lot of numbers to multiply and add.
We’ll start exploring how to generalise to any value of n in part 3.
The Christmas before last we played a family trivia game in which one question was: how many gifts were received in the 12 days of Christmas?
The answer is 364 and my daughter, working with her husband (fiancé at the time), got there first.
364 gifts, one gift for each day in a non-leap year (365 days). Every day except Christmas, you could say.
I’m not good at coming up with answers to this sort of question quickly, but I enjoy thinking about the underlying problem, different approaches, and in particular, the simplest and most general solution. This kind of recreational mathematics also often takes me down unexpected paths.
I can never remember the lyrics, so here’s a reminder of The 12 days of Christmas, a carol in which each each day, gifts are given to the recipient by a true love. The following omits a few days (verses) for brevity:
On the first day of Christmas, my true love sent to me, a partridge in a pear tree.
On the second day of Christmas, my true love sent to me, 2 turtle doves and partridge in a pear tree.
On the third day of Christmas, my true love sent to me, 3 french hens, 2 turtle doves, and a partridge in a pear tree.
On the fourth day of Christmas, my true love sent to me, 4 calling birds, 3 french hens, 2 turtle doves, and a partridge in a pear tree.
On the fifth day of Christmas, my true love sent to me, 5 golden rings, 4 calling birds, 3 french hens, 2 turtle doves, and a partridge in a pear tree.
…
On the eleventh day of Christmas, my true love sent to me, 11 pipers piping, 10 lords a-leaping, 9 ladies dancing, 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 french hens, 2 turtle doves, and a partridge in a pear tree.
On the twelfth day of Christmas, my true love sent to me, 12 drummers drumming, 11 pipers piping, 10 lords a-leaping, 9 ladies dancing, 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 french hens, 2 turtle doves, and a partridge in a pear tree.
For the purpose of solving the problem, we can ignore the actual gifts and just focus on the numbers, e.g. the two turtle doves becomes just 2 gifts of some kind, in this case, 2 lots of the same kind of gift (turtle dove) on day 2, or simply 2 lots of gift number 2.
A first intuition leads to adding all the gift numbers from 1 to 12 giving 78, but that’s only part of the answer.
On the first day we are given 1 gift (a partridge in a pear tree).
On the second day we are given 1 gift plus 2 more (a partridge in a pear tree, two turtle doves) giving 1+2 = 3 gifts.
On the third day we are given 1 gift plus 2 more plus 3 more (a partridge in a pear tree, two turtle doves, three french hens) giving 1+2+3 = 6 gifts.
And so on.
Of course, the ordering in the carol’s verses is the reverse of 1+2+3+…+n, given the order in which the gifts appear, but since 1+2+3+…+n = n+…+3+2+1, the summation is unaffected.
When we were all trying to come up with the right number that Christmas time, I couldn’t help but wonder whether there were simpler ways to arrive at this, more compact expressions. It turns out there are.
What interests me is this: finding the most compact mathematical expression that gives the same answer as the brute force row addition method above, whether the n in “The n days of Christmas” is 12 or 100. Imagine the time and effort required if there were 100 days of Christmas!
The second post will explore two approaches, first very briefly revisiting the row addition approach above, then moving on to column multiplication and addition of these products. It’s much easier to see this working than explain it abstractly.
The third post will generalise the approaches so far to any value of n, not just 12, via simple code in different programming languages.
The fourth post will present an approach involving the day 1 to 12 (or 1 to n) results as triangular numbers and the sum of these. I arrived at this after listening to a podcast by chance that helped me think about the problem in a different way. This approach allows the generalisation to any value of n with a mathematical expression rather than via programming.
The fifth post will present the most compact general solution for any value of n that I have found so far, arrived at by plotting and modelling the results. There will also be a short digression on rational numbers vs real numbers and problems arising from the representation of real numbers in computers.
The sixth post will summarise the approaches and their relative performance.
The seventh and final post will show that a potentially interesting link exists between the last two approaches, by employing a bit of calculus.
All of this in a single post would be a bit much. Several short posts seemed more appropriate.
Following on from my last post On errors in math and code, here’s an example (adapted from Khan Academy) of how an error can be caught with a small amount of symbolic prompting along the way towards the final numeric answer.
For the purpose of reading this post, it doesn’t matter so much whether you understand all the steps, more that taken together, the whole thing is complex enough that you can see how an error is possible. Even a simpler problem could be enough for this.
The circle of radius 4 above has the following equation:
where 16 is .
The green lines perpendicular to the x-axis each form the base of an equilateral triangle. For a given x value, the line extends the same amount, y, above and below the x-axis, so has length 2y, as shown below.
If you imagine many such triangles closely packed together over the circular base, the 3D shape at left below emerges with (a) The solid, showing a few equilateral triangles extending above the x-y plane. Note that, incidentally, in the example below, the circle’s radius is 1 not 4 as in our example (not important for how to approach a solution to the problem).
The problem: how to compute the volume of this 3 dimensional solid? The answer requires the use of integral calculus, which allows the area or volume of non-trivial functions to be computed.
To compute the volume of the 3D shape presented in the problem, we need to follow these steps:
Determine the triangle height by taking half of the yellow equilateral triangle, giving a right angle triangle, allowing us to apply the Pythagorean Theorem to determine the height. This is already shown in the yellow triangle above as the square root of 3 but we will show how to calculate it given a hypotenuse of length 2y and a base of half that (y).
Determine the area of a triangle first in terms of y, then x since we will be summing areas over a range on the x axis.
Calculate the volume of the 3D shape by summing the area of an “infinite number” of triangles by computing the definite integral over the appropriate x axis range.
Along the way I will show an error and how a sanity check could serve as a correction before proceeding to calculate the final answer.
Triangle height using the Pythagorean Theorem:
Triangle area in terms of y:
Triangle area in terms of x (from circle equation), substituting for y squared:
Volume by integration over area of triangles of width dx (delta x), where dx becomes very small, so there are “infinitely many” triangles.
This is almost right, but the bounds are wrong. Look at the circle at the start of this post. It spans -4 to 4 on the x axis, not 0 to 4. The volume should therefore be twice that calculated above. We get that from what follows, with the correct integration bounds.
But if just one half of the circle’s base is used for determining the volume, the math is simpler. Compare the last two solutions: in the first there are fewer numbers in the calculation, so less opportunity for error. If one half of the volume is computed, the whole volume is simply twice this, and that was what was initially in my mind; I simply forgot to multiply by two! Taking this into account gives:
So, a simple error (wrong bounds or not multiplying by two) was introduced in the very first step of the integration but did not become tangible until we started substituting numbers.
A graphing calculator application like Desmos first confirms the result of using the erroneous bounds and then the equivalence of the last two approaches:
After all that, the main point I’m making is that guidance during learning can be given along the way to a solution (in the textbook or online resource where the problem appears) by showing multiple choices for the equation itself, not the numeric answer, such as the first and last above along with some incorrect ones. This may be enough for someone to realise that a mistake has been made before proceeding to substitute numbers.
Online learning resources often use the multiple choice approach or the “just give the numeric answer” approach, e.g. to three decimal places or in terms of , but it seems less common to see a two stage process in which the correct equation must be selected followed by the numeric answer given. I wonder about the potential benefit of this, especially when first learning a specific, complex subject area in which mistakes are easy to make.
An argument against this is that it is better to allow failure and provide step-by-step guidance in the event of an incorrect answer, something Khan Academy excels at. Of course, this doesn’t guarantee that the same mistake won’t be made across similar problems.
People are sometimes mildly surprised when I say I go out to the garden at work or to a cafe on the weekend or outside at home to do a bit of recreational mathematics.
What sort of recreational mathematics? Often, filling in the many gaps in my knowledge usually using online resources (more on that later), almost always going down a rabbit hole. I’ll elaborate on the “rabbit hole” part in future posts.
To paraphrase Star Trek’s Dr McCoy, I’m a software engineer, not a mathematician dammit, but I truly think mathematics is humankind’s most amazing invention or discovery, depending upon your philosophical position (a rabbit hole of its own). I’ve had the sense that mathematics is important since at least my last year of school (1980) when I was first introduced to topics like complex numbers and calculus.
In school, I was a very average student, often below average, doing much better with study after leaving school. It is sometimes said that education is wasted on the young. There’s truth in that, because often, we’re not ready when we’re young. We have other interests, don’t see our possible futures well, don’t see the big picture, don’t have the right mentor. I’m 60 now, and have a slightly different perspective on the world than that of my teenage self.
Mathematics is the language of Science. Having worked in a research organisation for more than a decade, surrounded by people with much greater knowledge of math and science, having had kids go through school, wanting to help when they’ve struggled, just as I did: these things have motivated me to spend more and more time learning new things and re-learning old. But now I do it mostly for myself. Just because. I don’t really need another reason. Having said that, it is also important for my work.
The last of my two kids finished school a couple of years ago (although my daughter expressed interest in learning more math in 2023, so we did some calculus together, which was nice). Much of the way math is done in schools (at least in Australia) does not seem to have changed much in the 43 years since I was in school. The text books are similar, sure sometimes they’re PDFs on a laptop, and sometimes there are online and tools and resources with quizzes, but mostly it’s still done with pencil/pen and paper. You get graded on what you get right and learn (maybe) from the bits you lose points on. At least for my two kids, reinforcing quizzes seemed to be infrequently used, and often, people were left behind.
It’s no wonder that people talk about how they hate math in later life, given their school experience, which is a shame, because there is wonder in mathematics and it underpins a lot of what we take for granted (all our technology). Do we really need more politicians with Law degrees or more with science, engineering, mathematics? I’m reminded of a Star Trek episode where the Enterprise answers a distress call and beams down to the planet to find a civilisation whose technology is breaking down and no-one understands it anymore.
There are plenty of text books and some great books, such as Calculus and Pizza that I ate up (groan) several years ago. The Theoretical Minimum does a reasonable job of giving you enough math (mostly calculus) to do classical physics. But in my view, one of the best resources (for math and science) is Khan Academy. What I love about it is that you watch a short video or two, perhaps read a short reinforcing article, then you are presented with 4 problems. The free app means that all I need is my phone, a notepad and a pen (slightly ironic given my comment about pen and paper above).
A rule that I rarely break is that no matter how many sets of 4 problems (they change each time, with some eventual recycling) I have to go through, I don’t proceed to the next section until I get them all right. It’s about competence, mastery. I’m not in a hurry, which is good, because I’m the slow plodding type. If I do break that rule it’s because I’ve done many sets of 4 problems for the section but I’ve just made careless mistakes, and I believe I fully understand the concepts. I’ve broken that rule once in the last couple of years, and it still didn’t feel right, even though I knew exactly what I had done wrong and why I shouldn’t have made the mistake. I may even have done an (almost) identical problem in a previous set of 4, and got that right.
This issue of mistake-making while solving problems is something that increasingly interests me.
I’ve been a software engineer for more than 25 years and coding for fun for more than 40 years, so I’m used to the idea of writing code and writing additional code to test that. There are various approaches to software verification and that’s a topic I could write a lot about, and spend a lot of time doing and talking about as a practitioner of software development in my workplace. For example, to illustrate the idea, given this trivial function in the Python programming language that returns the cube of a number ():
def cube(x):
return x*x*x
one or more functions, procedures actually, can be written to test this by asserting that certain expressions must be true. Here is one such procedure:
def test_cube():
assert cube(2) == 8
assert cube(4) == 4**3
assert [cube(n) for n in range(1, 101)] == \
[n**3 for n in range(1, 101)]
which, in words says:
It must be true that applying the function cube to 2 equals 8.
It must be true that applying the function cube to 4 equals 4 raised to the power of 3.
It must be true that applying the function cube to each number in the range 1 to 100 is the same as raising each corresponding number to the power of 3.
If any of these assert statements yield an expression that is not true, the test will fail, often with obvious red text in the output. An important benefit is that if the code under test is later modified (e.g. to make it run faster) a test should detect failures if bugs are inadvertently introduced.
As an aside, in case you’re wondering, 101 is not included in the range above, a so-called exclusive range in Python. As another aside, the odd looking code in test_cube:
[cube(n) for n in range(1, 101)]
is a so-called “list comprehension” which is essentially a loop that returns a list, in this case:
[1, 8, 27, 64, 125, 216, 343, 512, ..., 1000000]
A real-life function I wrote recently computes the checksum of the bytes of a sensor packet allowing it to be compared to the expected checksum (designed to guard against transmission errors between sensor and computer) where the expected checksum is encoded as the last byte of the packet. In this case I could write the checksum function knowing that there were known good expected results against which a test function could be written. It’s not always that straightforward though.
Returning to Khan Academy and mathematics, while often a problem will be presented and a numeric answer required, it is not uncommon for the problem writers to acknowledge the difficulty level such that multiple choice symbolic answers are given, often in the form of a non-trivial function. The learner may then realise that none of the answers given matches his/hers, prompting a check of the steps leading up to a solution, precisely what you are forced to do when code you have written fails to pass a test.
Could an increased emphasis upon such approaches help learners of mathematics? I suspect so, and I plan to explore this theme in future posts.