As we are steadily approaching the Holidays, I’ve decided to contribute a post in the spirit of Christmas!

Before we jump in though, I think a *quick *introduction to who it is that is writing this is in order:

To start, I was born and raised in Montreal, Canada and I currently work/research on a regular basis with Deyan. My background is in electrical engineering, but that doesn’t mean that’s where my only interests are! There are so many fascinating topics to explore in this world and limiting ourselves to just one is a shame. Neuroscience, Biology, Physics, Computer Science, Financial Markets – these are all interesting areas and fields which I believe could greatly benefit from increased interaction between one another. Anyway, so what am I doing here? Simply put, I like the concept of contributing information on a regular basis. As such, I’ve decided to pitch in occasionally as a guest blogger to this 365 day journey of continuous blogging – quite the feat!

**Now, back to the problem.**

As those bright colorful lights start appearing everywhere, it is a telltale sign that it is that family-time of the year again where I’m sure retailers notice a nice bump in sales. Indeed, it is Christmas time, and with that come Christmas carols. This is where our journey begins:

**“The Twelve Days of Christmas”** is one of those Christmas time songs that is hard to miss. If you have never heard or don’t remember which I’m referring to, give it a quick listen here. From Wikipedia, it is “an English Christmas carol that enumerates in the manner of a cumulative song a series of increasingly grand gifts given on each of the twelve days of Christmas”. Knowing that and having heard the song, the question now is,

**What is the sum total of gifts given?**

Now, even with just a quick read through Wikipedia, we can find that the answer is **364**. However, without manually going through each day and tediously summing everything up, how can we get to that answer? What if the song lasted for 212 days? How many gifts would we get in total then?

**Using a computer**

If you were going to write a program to do this sum, you could easily implement the calculation using a recursive function. It would look something like this (pseudo-code):

function GiftCalculator (integer Days) {
if Days = 1 then
return 1
else
return (Days + GiftCalculator(Days - 1))
end if
}

Very simple, very effective, but start plugging in big values and this method will start taking up a lot of time (and memory!). Also, this is essentially the same as doing it tediously by hand, but having a computer do the grunt of the work. This does not TRULY simplify obtaining the answer.

What we are looking for is to obtain a one line function expression. A secret formula that, when supplied with a value of “x”, spits out a resulting value for “f(x)” which corresponds to the total number of gifts given. Something of the form

**Where is the pattern?**

Listening to the Twelve Days of Christmas, it is very easy to detect that there is a pattern embedded into the song. It is this same pattern that makes it possible for our brains to predict the progression of the song thus making it possible to jump in and sing along with the carolers (provided you are in the mood – I’m looking at you Ebenezer Scrooge). So, how can we formulate this?

**First thing’s first, it’s definitely a sum. **Everyday of the song, you receive an amount of gifts that is equivalent to that day’s position (relative to the initial day) plus the amount of gifts received on the previous day. So, on day 4, you would receive 4+(3+2+1) = 10 gifts. What we are then doing is adding up all the gifts received on everyday to obtain a value for the total amount of gifts. Thus, it is a sum and will require the use of the good old symbol.

Let’s start of by finding the value of gifts obtained on any given day. As we saw earlier, Day 4 was 4+3+2+1. Following this, Day 5 would be 5+4+3+2+1, which is the same as 1+2+3+4+5. Examining this, we can see that the amount of gifts received on any given day is equivalent to the sum of all the “day numbers” up to and including the day in question. This can be formulated as:

where “d” refers to the day for which we want to know the amount of gifts and “i” range of integers between 1 and “d”.

This may look a little intimidating if you’ve never worked with sums before, but if you have, you will recognize that this is a geometric series whose result is . Just to double check, plugging in with a value of 4 for “d”, we obtain which is what we found before. Great!

**Next**, we want the sum of **ALL the gifts** over the twelve day period. So, we want the value obtained for Day 1 + Day 2 + Day 3 + … + Day 12. Once again, this is similar to the sum we did above and can be written as:

where “t” refers to the total amount of days (which in our case is 12), “d” refers to the range of days between 1 and “t” and where f(d) corresponds to the number of gifts received on day “d”.

Now, we already know what f(d) is, we found it previously. It is: So let’s go ahead and replace that into our above equation. Doing that we will obtain the following double-sum:

As we know what the formula is for the inner sum, this can also be written as:

Further simple simplification gives us:

Which we can then break up as a sum of two sums:

Looking at the above equation, we are familiar with the term on the right and know that it is equivalent to as we did above. However, what about the term? Well, to our delight this is also a geometric series which can be expressed as . Knowing this, we can return to our above equation and replace the sums by their corresponding algebraic expressions:

Finally, the last step is to simplify to obtain a nicer looking algebraic equation:

**Now, for the moment of truth, let’s plug in a value of 12 for “t”**

**…and voila! 364, as expected!**

**A more creative approach**

Great, we have found the answer we are looking for, but was there a different, easier or maybe more intuitive manner to get to this result? Whether the following approach will fulfill those criteria, I can’t claim for sure. When presented to me, it seemed more intuitive for my way of thinking, but that may not apply the same way for others.

Either way, here it is.

Let’s start by making a picture representation of what is indeed happening in the song:

As we can see, as we progress through the days, the row *values increase* by 1, but the *row lengths* decrease by 1. This leads to a sum of the form , which with a bit of pondering can be formulated as:

This is then simplified as follows,

**which is what we obtained previously!**

#### And that is it, there you have it. You can now calculate the total amount of gifts given for any variation to the song’s length in days with a quick and simple formula!

**A little additional interesting tangent to this, the sum total of the **COST** of all the gifts in the “Twelve Days of Christmas” has been used as a tongue-in-cheek economic index. This year’s calculated cost is 27,673.22 USD. Check out how it compares to other years here.