Exercise 2.1: The Twelve Days of Christmas


The Twelve Days of Christmas is an 18th century English song traditionally sung during the Christmas holiday. In the song, the singer’s true love gives gifts each day. Here are the gifts on the first three days:

  • Day 1: A partridge in a pear tree.

  • Day 2: Two turtle doves and a partridge in a pear tree.

  • Day 3: Three French hens, two turtle doves, and a partridge in a pear tree.

The first day features one gift, the second three, and the third six. The fourth has ten, and so on. This extravagant giving continues for twelve days.

a) Write code to compute the total number of gifts given in the n days of Christmas. Do this using two for loops. Give your result for n = 12 and for n = 365.

b) It can be shown that

\begin{align} \sum_{i = 1}^m i = \frac{m(m+1)}{2}. \end{align}

Use this fact to do the calculation with a single for loop. If you feel like it, prove the above relation (not a coding but fun).

c) While we’re having non-coding fun, derive an expression for the total number of gifts given in n days of Christmas. (This is substantially harder than part (b) and you are not expected to do this; it’s just a fun challenge. As a hint, you may find it useful to consider the expansion of \((m - 1)^3\).)