‘Tis the season for a holiday tune, and what better classic is there than The Twelve Days of Christmas?

Some old-timey gifts

And how lucky the narrator is to have such a thoughtful true love, no? So many gifts! Twelve drummers, eleven pipers, … For a total of 78 gifts! Since as we all know1:

12+11+...+1=∑i=1Ni=N(N+1)2 12 + 11 + ... + 1 = \sum_{i=1}^{N}{i} = \frac{N (N + 1)}{2}

As a quick aside, we can observe the above is true via some rearranging:

∑i=1Ni=1+2+...+(N−1)+N=(1+N)+(2+(N−1))+...=(1+N)+(1+N)+...=N(N+1)2 \begin{aligned} \sum_{i=1}^{N}{i} &= 1 + 2 + ... + (N-1) + N \\ &= (1 + N) + (2 + (N-1)) + ... \\ &= (1 + N) + (1 + N) + ... \\ &= \frac{N (N + 1)}{2} \end{aligned}

That’s a bit of a hand wave, so perhaps we should prove it (i.e., just to see if I remember how induction works).

The base case (N = 1) is clear:

1(1+1)2=∑i=1N=1i=1 \frac{1 (1 + 1)}{2} = \sum_{i=1}^{N=1}{i} = 1

And if we assume N holds, then it imples N + 1:

∑i=1N+1i=∑i=1Ni+(N+1)=N(N+1)2+(N+1)=N(N+1)2+(2N+2)2=(N2+N)+(2N+2)2=N2+3N+22=(N+1)(N+2)2QED \begin{aligned} \sum_{i=1}^{N+1}{i} &= \sum_{i=1}^N{i} + (N+1) \\ &= \frac{N(N + 1)}{2} + (N+1) \\ &= \frac{N(N + 1)}{2} + \frac{(2N + 2)}{2} \\ &= \frac{(N^2 + N) + (2N + 2)}{2} \\ &= \frac{N^2 + 3N + 2}{2} \\ &= \frac{(N + 1) (N + 2)}{2} & QED \end{aligned}

Ok, so anyway, what an impressive Christmas haul! But wait… if you listen to the lyrics literally, is that right?

On the first day of Christmas, my true love gave to me: A partridge in a pear tree.
On the second day of Christmas, my true love gave to me: Two turtle doves, and a partridge in a pear tree.
…

This sugar daddy is handing out a new partridge each day! This means the total number of gifts is actually more like:

1+(1+2)+(1+2+3)+...=∑i=1N∑j=iij 1 + (1 + 2) + (1 + 2 + 3) + ... = \sum_{i=1}^N{\sum_{j=i}^{i}{j}}

Now, if we wanted to compute the total, it would be straightforward do it with something like this:

N = 12
sum(sum(j for j in range(1, i + 1)) for i in range(1, N + 1))
# 364

(incidentally, one for every day of the year except one!) but where’s the fun in that?! We need a closed-form solution, in case we have, you know, some future Christmas season with 1e10 days or whatever (good luck with that quadratic loop then!).

So, it’s conveneient to write the above as a triangular matrix, where rows are days, and columns are gifts:

[100…120123⋮⋱] \begin{bmatrix} 1 & 0 & 0 & \ldots \\ 1 & 2 & 0 & \\ 1 & 2 & 3 & \\ \vdots & & & \ddots \end{bmatrix}

And we want the sum of the whole thing. Now, looking down columns rather than across rows, it becomes clear that the sum can be written like:

N+2(N−1)+...+N=∑i=1Ni(N−i+1)=(N+1)∑i=1Ni−∑i=1Ni2 \begin{aligned} N + 2 (N - 1) + ... + N &= \sum_{i=1}^{N}{i (N - i + 1)} \\ &= (N+1) \sum_{i=1}^{N}{i} - \sum_{i=1}^{N}{i^2} \end{aligned}

From the previous section, we know the sum of the first N integers, but what about the sum of their squares? Well, this gets a little tricky. Suppose we consider the following expression (which, I shamelessly stole from Stack Overflow):

∑i=1N(i+1)3−i3 \sum_{i=1}^{N}{(i + 1)^3 - i^3}

This can be looked at two ways: first, as a “telescoping series”:

∑i=1N(i+1)3−i3=23−13+33−23+...+(N+1)3−N3=(N+1)3−1=N(N2+3N+3) \begin{aligned} \sum_{i=1}^{N}{(i + 1)^3 - i^3} &= \cancel{2^3} - 1^3 + \cancel{3^3} - \cancel{2^3} + ... + (N+1)^3 - \cancel{N^3} \\ &= (N + 1)^3 - 1 \\ &= N(N^2 + 3N + 3) \end{aligned}

and secondly, expanded:

∑i=1N(i+1)3−i3=∑i=1N3i2+3i+1=3∑i=1Ni2+3∑i=1Ni+∑i=1N1=3∑i=1Ni2+3N(N+1)2+N \begin{aligned} \sum_{i=1}^{N}{(i + 1)^3 - i^3} &= \sum_{i=1}^{N}{3i^2 + 3i + 1} \\ &= 3 \sum_{i=1}^{N}{i^2} + 3 \sum_{i=1}^{N}{i} + \sum_{i=1}^{N}{1} &= 3 \sum_{i=1}^{N}{i^2} + \frac{3N(N + 1)}{2} + N \end{aligned}

Which means we have

N(N2+3N+3)=3∑i=1Ni2+3N(N+1)2+N∑i=1Ni2=13(N(N2+3N+3)−3N(N+1)2)=16(2N3+3N2+3N)=N(N+1)(2N+1)6 \begin{aligned} N(N^2 + 3N + 3) &= 3 \sum_{i=1}^{N}{i^2} + \frac{3N(N + 1)}{2} + N \\ \sum_{i=1}^{N}{i^2} &= \frac{1}{3} (N(N^2 + 3N + 3) - \frac{3N(N + 1)}{2}) \\ &= \frac{1}{6} (2N^3 +3N^2 +3N) \\ &= \frac{N(N + 1)(2N + 1)}{6} \end{aligned}

So going back to the formula for the total number of gifts after N days:

Tgifts=N+2(N−1)+...+N=(N+1)∑i=1Ni−∑i=1Ni2=N(N+1)22−N(N+1)(2N+1)6=N(N+1)(3(N+1)−(2N+1)6)=N(N+1)(N+2)6 \begin{aligned} T_{gifts} &= N + 2 (N - 1) + ... + N \\ &= (N+1) \sum_{i=1}^{N}{i} - \sum_{i=1}^{N}{i^2} \\ &= \frac{N(N+1)^2}{2} - \frac{N(N + 1)(2N + 1)}{6} \\ &= N (N+1) \left( \frac{3(N+1) - (2N+1)}{6} \right ) \\ &= \frac{N (N+1) (N+2)}{6} \end{aligned}

Which gives us our expected 364 for N=12! And, for N=1e10, something like:

1030+3×1020+2×106=10000000003000000000200000000006 \frac{10^{30} + 3 \times 10^{20} + 2 \times 10}{6} = \frac{1000000000300000000020000000000}{6}

You know, in case you need it. Merry Christmas!


  1. I actually subjected my friends to this problem during Christmas Party trivia. Remarkably, some of them came back the next year. ↩︎