## Calling all programmers
Code a puzzle 4 Xmas
_[The Twelve Days of Christmas](https://en.wikipedia.org/wiki/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**
#math #puzzle #invitation #teaser #programmers #programming #code #christmas #days
@Pat I assure you do not have to to go back to the 70s to write a solution. 😉
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)
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.)
My Answer (spoilers)
@Pat
I don't use AWK. This said, the code seems legit, but I cannot replicate it. There must be some shenanigan I don't know how to fix.
The thing is: when I execute the line it stalls indefinitely.
My Answer (spoilers)
>"when I execute the line it stalls indefinitely."
AWK is designed to mostly process text, either from a file or from stdin (standard input). It's a text stream processor.
If you specify a file name, it takes input from that file; if you don't specify a file name, it takes input from stdin. As it's presented in the toot, there is no file name so it takes input from stdin, in this case from the keyboard.
So when it "stalls" just enter numbers in your keyboard and it will spit out the answers.
You can also specify a list of numbers (one per line) for input in a file like this:
awk ‘n=0;{for (i=1; i<=$1; i++) n=n+(i^2+i)/2;print n}’ numbers.txt
and it will spit out the answers for all of the numbers from the file "numbers.txt". The "$1" in the code is the parameter for the first input field of a line from the input -- in this case just the one field, the number you input from stdin.
(For what AWK was designed for, I'm surprised it had an exponent function.)
My Answer (spoilers)
@Pat
Brilliant solution.
With extra points because I laughed out loud when I read “I also found this on the internet, so I know it’s right.” 😆
Also, I’m a little reluctant to believe that you didn’t know already that this was an arithmetic progression. May I be forgiven if I’m wrong.
Once again thank you for taking the time to participate.
Wish you a good new year.
My Answer (spoilers)
I guessed it was some kind of series that was likely well-known to mathematicians, but the term "arithmetic progression" didn't come mind at all. Now that you've said it, yeah, it's a progression and it's additive (arithmetic), so it makes sense.
As I said, I'm not a math person. My last algebra class was when Nixon was still in office! I'm surprised I even remembered how to do the simple algebra in that solution.
You're forgiven, of course.
Have a happy new year.
My Answer (spoilers)
Just for fun here's a solution in BASH...
n=0;for ((i=1;i<=1000;i++)); do n=$((n+(i*(i+1)/2)));echo "$i - $n";done
Prints the solution for the first 1000 days.
BASH has exponentiation (**) but this shows to how it would be done without it -- (n^2+n)/2 becomes n(n+1)/2.
Again, I'm sure there must be some shortcut for this, but I can't figure one out.
Also, I looked up "arithmetic progression" and this doesn't fit the definition (each number differs by a fixed amount). It looks like an exponential function or maybe an "arithmetico–geometric sequence".
My Answer (spoilers)
@Pat
> I looked up “arithmetic progression” and this doesn’t fit the definition
Of course it does not fit... Did I say it was an arithmetic progression? OG, what was I thinking?!
Sorry. Now I'm focused. It's just a triangular numbers sequence with accumulation of all previous results.
My Answer (spoilers)
@Pat
> My unconscious brain keeps interrupting my day with ideas on this…
OMG! What have I done? I'm sorry, really didn't mean to... 😅
> the solution should be the formula for the volume of a tetrahedron
Wouldn't it be nice if it could be so simple? 😃
> Do you already know what the general formula is?
Nope. But I trust you will find it. Some authors say that obsessive thinking is the start of the 'Eureka' moment. 🤭