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

Follow

(as )

2. Generalizing the function for any number of days:

function allgifts2(days)
dg = [] # daily gifts storage
for d in 1:days
d = sum(1:d) # daily gifts from day 1 to days
push!(dg, d) # storing each day gifts
end
print(sum(dg)) # showing total number of gifts in storage
end

There are actually 13 days from 25 Dec to 6 Jan

Let’s add to the list Thirteen puzzlers puzzling

> allgifts(13)

455

Did The Wise Men (Magi) Arrive 12 Days After Jesus’ Birth?
reasonsforhopejesus.com/did-th

Happy Three Kings Day

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.