20

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257

4 Answers4

24

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"}}
    ]
]

rm -rf
  • 88,781
  • 21
  • 293
  • 472
17

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
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
10

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

enter image description here

Merry Christmas

Visualizing the sum of triangular numbers from the song:

enter image description here

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}$

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • @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 09:55
3

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

cormullion
  • 24,243
  • 4
  • 64
  • 133