20
$\begingroup$

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.

$\endgroup$
7
  • 1
    $\begingroup$ 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...:) $\endgroup$
    – cormullion
    Commented Dec 24, 2013 at 8:36
  • 2
    $\begingroup$ Oh, I see he's not the only one... I think you're both wrong, but I like it better - more chaotic! $\endgroup$
    – cormullion
    Commented Dec 24, 2013 at 10:17
  • $\begingroup$ Sum[j, {i, n}, {j, i}] $\endgroup$
    – Rojo
    Commented Dec 24, 2013 at 17:34
  • 2
    $\begingroup$ (1/6)*n*(1 + n)*(2 + n) $\endgroup$
    – Rojo
    Commented Dec 25, 2013 at 4:29
  • 1
    $\begingroup$ @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. $\endgroup$
    – Rojo
    Commented Dec 25, 2013 at 12:03

4 Answers 4

24
$\begingroup$

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

$\endgroup$
5
  • 6
    $\begingroup$ Awesome answer. You wrote it in "just 30 seconds". I hope? :) $\endgroup$
    – cormullion
    Commented Dec 24, 2013 at 8:41
  • $\begingroup$ @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! $\endgroup$
    – ubpdqn
    Commented Dec 24, 2013 at 13:12
  • $\begingroup$ +1 for giving me a fantastically clever new kind of reason to upvote $\endgroup$ Commented Dec 24, 2013 at 15:06
  • $\begingroup$ Stephen! Stephen! Look! I just met your soulmate! $\endgroup$ Commented Dec 24, 2013 at 15:38
  • $\begingroup$ @ubpdqn at least one would hope humans are not gifts :-( $\endgroup$ Commented Dec 24, 2013 at 19:50
17
$\begingroup$

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
$\endgroup$
8
  • 3
    $\begingroup$ Brilliant! I particularly like "And Apart Range Inner Part Re" $\endgroup$
    – ubpdqn
    Commented Dec 24, 2013 at 13:16
  • 1
    $\begingroup$ You have a few syntax errors in there $\endgroup$ Commented Dec 24, 2013 at 15:42
  • 2
    $\begingroup$ @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! $\endgroup$ Commented Dec 24, 2013 at 15:50
  • 1
    $\begingroup$ Can this be done algorithmically using the pronunciation dictionary and finding the closest match among the known words in the Mathematica language? $\endgroup$
    – bill s
    Commented Dec 25, 2013 at 13:46
  • 1
    $\begingroup$ @bills, it would be an interesting challenge, but probably great fun to play with once it was done! $\endgroup$ Commented Dec 28, 2013 at 18:12
10
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ @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. $\endgroup$
    – ubpdqn
    Commented Dec 25, 2013 at 9:55
3
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.