Calling all programmers

Code a puzzle 4 Xmas

The Twelve Days of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me:

  • A partridge in a pear tree

On the second day of Christmas (26th December), my true love sent to me THREE presents:

  • Two turtle doves
  • A partridge in a pear tree

On the third day of Christmas (27th December and so on) my true love sent to me SIX presents:

  • Three French hens
  • Two turtle doves
  • A partridge in a pear tree

This carries on until the the twelfth day of Christmas, when my true love sends me:

  • Twelve drummers drumming
  • Eleven pipers piping
  • Ten lords a-leaping
  • Nine ladies dancing
  • Eight maids a-milking
  • Seven swans a-swimming
  • Six geese a-laying
  • Five gold rings
  • Four calling birds
  • Three French hens
  • Two turtle doves
  • A partridge in a pear tree

Puzzle Author: Stephen Froggatt

  1. Strict, hard coded, solution:
    ```
    function allgifts()
    dg = [] # daily gifts storage
    for d in 1:12
    d = sum(1:d) # daily gifts from day 1 to 12
    push!(dg, d) # storing each day gifts
    end
    print(sum(dg)) # showing total number of gifts in storage
    end

> allgifts()

364

2. Generalizing the function for any number of days:

I’ll put here my solution (in Julia) on the twelfth day

```
Meanwhile, I invite you all to post in the comments some solution coded in the programming language of your choice

@mc

The solution depends on whether or not the partridges know how to play the drums.

@Pat I assure you do not have to to go back to the 70s to write a solution. πŸ˜‰

@mc

I first went to Mathlab, but then thought it would be more fun to use something completely unsuited to the task, like AWK or BASH.

Are you looking for code for a function similar to the hard-coded solution in your example, or are you looking for some kind of mathematical shortcut using factorials or calculus or something?

(I’m not a math gal but I’m sure this must be a well-known series in mathematics, with the name of some old mathematician tacked on to it.)

@Pat
You may find it hard to believe, but I’m making this for fun, in the spirit of the season.
As you’ve been so kind to offer me the choice, AWK please. πŸ˜ƒ
(Yes, it’s similar to “a well known series in mathematics, with the name of some old mathematician…” 🀭 )

My Answer (spoilers) 

@mc

Here’s the solution in awk:

awk ‘n=0;{for (i=1; i<=$1; i++) n=n+(i^2+i)/2;print n}’

I don’t know if there is a mathematical shortcut for the whole function – I couldn’t figure one out. But I did figure out a shortcut for the summation of a sequence of numbers…

If you add the first and last numbers in the series, you get 1+n. The same for the second number plus second to last number, etc., because they increment/decrement respectively. If you keep going, you meet in the middle of the sequence – half way. So, the sum of all the numbers is the same as n+1 multiplied by half the number of numbers in the sequence, or (n+1)(n/2). I tested it and it works with an odd number also. A little algebra, (n+1)(n/2)=(n^2+n)/2. (I also found this on the internet, so I know it’s right.) If you write it using a language that doesn’t have an exponent function, you could just use (n+1)(n/2). (In fact, that might be more efficient/faster, I didn’t test it.)

So, using that shortcut, it’s just a simple loop to “sum the sums”. If you use a language with a summation function it would be even easier.

Without the shortcut you got to nest loops or use an array or something.

(ps - I’m not really a programmer, i.e., I don’t do it for a living, but I answered the “call” anyway.)

Follow

My Answer (spoilers) 

@Pat
Oh, and thank you for answering the “call”. πŸ‘

Β· Β· 0 Β· 0 Β· 0
Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.