# How many Christmas gifts?

This question is based on the old song, The Twelve Days of Christmas

In a comment made on a recent question, Yves Klett remarked, "no seasonal questions this year?" This reminded me of one I solved six years ago. It's very easy, but has the virtue of giving a pretty expression for the general case.

Can you get Mathematica to tell you how many gifts True Love give over the 12 days of Christmas? Better yet, can you get a general expression for how many he would he give over a gift-giving period of n days?

I'm not going to post my answer unless no one posts an equivalent answer in the next 24 hours. I will be very surprised if that happens.

### Update

My interpretation of the song is on each day True Love repeats each gift from the previous day and adds a new gift with multiplicity equal to the day index. That means 364 gifts for 12 days. I'm looking for the general formula for $n$ days. It's still an easy problem, but the general formula is rather nice.

-
When my son heard this, he interpreted it more literally: "what does he do with 12 partridges and 12 pear trees, 22 turtle doves...?". It would make a better illustration...:) –  cormullion Dec 24 '13 at 8:36
Oh, I see he's not the only one... I think you're both wrong, but I like it better - more chaotic! –  cormullion Dec 24 '13 at 10:17
Sum[j, {i, n}, {j, i}] –  Rojo Dec 24 '13 at 17:34
(1/6)*n*(1 + n)*(2 + n) –  Rojo Dec 25 '13 at 4:29
@m_goldberg if noone answered yet, I'll do it after Christmas. Today I'd feel embarrassed to be the one writing the serious uncreative answer that gets accepted. –  Rojo Dec 25 '13 at 12:03

## 4 Answers

The math in this one is too simple, which takes the fun out of it. However, if we could write some New Kind of Sentient Code™, it might look like this:

WolframAlpha[StringJoin@
Riffle[
First@WolframAlpha["12 days of Christmas", {{"Result", 1}, "ComputableData"}] /.
{_, _, gift_} :> First@StringSplit@gift /. "a" -> "one" // Flatten // Rest,
" plus "],
{{"Result", 1}, "NumberData"}]
(* 78 *)


But why stop there? Surely a sentient code also ought to know that you'd like to see what the gifts look like...

{quantity, gift} = Transpose@
Rest@Cases[WolframAlpha["12 days of Christmas", {{"Result", 1}, "ComputableData"}],
{_, _, gift_} :> StringSplit[StringReplace[gift, " (" ~~ __ -> ""], " ", 2],
Infinity] /. "a" -> "one"


Using the Google images API mentioned here,

toNumber = ToExpression@First@WordData[#, "Synonyms", "List"] &;
getImage = With[{results = Import["https://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgtype=photo&q=" <> StringReplace[#, " " -> "%20"], "JSON"]},
Import["url" /. ("results" /. ("responseData" /. results))[[1]]]] &;


we can see the gifts given and a running counter of total gifts till the nth day:

Module[{accum = 0, days = With[{d = "Dec 25 2013"},
DateString[#, {"MonthName", " ", "Day"}] & /@ DateRange[d, DayPlus[d, 12]]]},
TableForm[
MapThread[
{# <> " " <> #2, ImageResize[getImage[# <> " " <> #2], {100, Automatic}],
accum = toNumber@# + accum} &,
{quantity, gift}
],
TableHeadings -> {days, {"Gift", "Image", "Total gifts"}}
]
]


-
Awesome answer. You wrote it in "just 30 seconds". I hope? :) –  cormullion Dec 24 '13 at 8:41
@rm-rf agree with cormullion this is awesome...in one blow extracts nice images, reminds us of the lyrics, and establishes what is the twelefth day...and gives the cumulative count...my wife contends there are only 12 gifts and that humans are not gifts but their service is "a" gift...seasons greetings! –  ubpdqn Dec 24 '13 at 13:12
+1 for giving me a fantastically clever new kind of reason to upvote –  Jacob Akkerboom Dec 24 '13 at 15:06
Stephen! Stephen! Look! I just met your soulmate! –  belisarius Dec 24 '13 at 15:38
@ubpdqn at least one would hope humans are not gifts :-( –  Carl Witthoft Dec 24 '13 at 19:50

I tried this but it didn't seem to work:

12 Do Run Most TrimmedMean;
11 Pi Plus Pi Pink;
10 Log Selection;
9 LatticeData;
8 Mode Sum Missing;
7 Sow On Assuming;
6 Glaisher Line In;
5 GoldenRatio;
4 Column Band;
3 For In Share N;
2 Total Div;
And Apart Range Inner Part Re

-
Hahahahahahahah –  Rojo Dec 24 '13 at 13:04
Brilliant! I particularly like "And Apart Range Inner Part Re" –  ubpdqn Dec 24 '13 at 13:16
+1 this is so clever! –  cormullion Dec 24 '13 at 14:42
You have a few syntax errors in there –  belisarius Dec 24 '13 at 15:42
@belisarius, I was tempted to try and make it syntactically correct but a) it was too difficult and b) I had wasted too much time on it already! –  Simon Woods Dec 24 '13 at 15:50

In the spirit of the question, Christmas (and triangular numbers)...done at short notice (apologies for imperfection) and perhaps to motivate more creative people.

Seasons greetings and peace to all.

Notebook with images is here. Sadly a large file but has images. As rm-rf observes $N=\frac{12\times13 }{6}=78$ is a little uninspiring...I am using the interpretation of accumulating gifts as 1 +2 +3+4+...+12. The song has always confused me a little as it could also be: 1 + (1+2)+ (1+2+3)+...+78=364. That's a lot of poultry, milk, people but at least only 12 drummers.

UPDATE

To answer m_goldberg puzzle: it is straightforward if $n=2 j$. As the sum of an even number of triangular numbers can be grouped in pairs: (1,3), (6,10), (n(n-1)/2,n(n+1).2), yielding: $\sum T_n =4\frac{j(j+1)(2j+1)}{6}=\frac{2j(j+1)(2j+1)}{3}$, so n=12-> j=6->4 x 91=364...an obvious nice visualization combining two triangular patterns into squares presents itself...sadly Christmas eve dinner awaits...for odd number n just go below and add last triangular number or go above and subtract...food

Merry Christmas

Visualizing the sum of triangular numbers from the song:

Merry Christmas to all and too all good night from the antipodes

Completing the answer: The odd n case: $\sum T_n = \frac{(n-1)(n+1)n}{6}+\frac{n(n+1)}{2}=\frac{n(n+1)(n+2)}{6}$

Rearranging the even result: $\frac{4j (j+1)(2j+1)}{6} =\frac{2j (2j+1)(2j+2)}{6}=\frac{n(n+1)(n+2)}{6}$

The same result and the beautiful formula m_goldberg desired albeit in an ugly manner...the fun was in the visualization....

THE NUMBER OF GIFTS for n days : $\frac{n(n+1)(n+2)}{6}$

-
@m_goldberg I see Rojo has the formula. I updated, in my awkward way (though there are a number of ways to derive result). Typing on iPhone and a little too much cheer made my update challenging. I will correct any errors when I have ability. –  ubpdqn Dec 25 '13 at 9:55

I wish I'd written the other answers. This is all I could manage:

 numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"};
Total[
Cases[
Flatten[
StringSplit[#] & /@
{"One Partridge in a Pear Tree", "Two Turtle Doves",
"Three French Hens", "Four Calling Birds", "Five Golden Rings",
"Six Geese a-Laying",
"Seven Swans a-Swimming", "Eight Maids a-Milking",
"Nine Ladies Dancing", "Ten Lords a-Leaping",
"Eleven Pipers Piping", "Twelve Drummers Drumming"}
] /. Dispatch[
Table[
numbers[[i]] -> i, {i, 1, Length[numbers]}]
], x_Integer :> x]
]


78

The GoldBerg Variation:

Total[
Cases[
Flatten[
StringSplit[#] & /@
{"One Partridge in a Pear Tree", "Two Turtle Doves",
"Three French Hens", "Four Calling Birds", "Five Golden Rings",
"Six Geese a-Laying",
"Seven Swans a-Swimming", "Eight Maids a-Milking",
"Nine Ladies Dancing", "Ten Lords a-Leaping",
"Eleven Pipers Piping", "Twelve Drummers Drumming"}
] /. Dispatch[
Table[
numbers[[i]] -> Total[ Range[i]], {i, 1, Length[numbers]}]
], x_Integer :> x]
]


364

-