## The Twelve Days Of Christmas

### December 25, 2012

You all know the old song:

On the first day of Christmas my true love gave to me a partridge in a pear tree.

On the second day of Christmas my true love gave to me two turtle doves and a partridge in a pear tree.

On the third day of Christmas my true love gave to me three French hens, two turtle doves and a partridge in a pear tree.

On the fourth day of Christmas my true love gave to me four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the fifth day of Christmas my true love gave to me five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the sixth day of Christmas my true love gave to me six geese a-laying, five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the seventh day of Christmas my true love gave to me seven swans a-swimming, six geese a-laying, five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the eighth day of Christmas my true love gave to me eight maids a-milking, seven swans a-swimming, six geese a-laying, five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the ninth day of Christmas my true love gave to me nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the tenth day of Christmas my true love gave to me ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the eleventh day of Christmas my true love gave to me eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

On the twelfth day of Christmas my true love gave to 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 golden rings, four calling birds, three French hens, two turtle doves and a partridge in a pear tree.

Your task is to write a program that determines how many gifts were given in N days, and use it to compute the number of gifts given in 12 days. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

This problem statement leaves some ambiguity, as the number of gifts given in N days depends exactly on which of the 12 days gifts were given. In addition to the parameter N, a consecutive range or non-consecutive set of days must be specified in order to know how many gifts were given in total.

Gifts that day: 2**n-1 or n(n+1)/2

Gifts total: n**2

At least I think that is what it is, assuming the following:

1. The number of gifts received is not modular, and

2. The gifts received = n + n-1 + n-2 + n-3 + … + 1.

An edit to my previous post, 2**n-1 doesn’t work, sorry.

Not so elegant solutions, the first is recursive,

— recursive

crimbo presents 0 = presents

crimbo presents days = crimbo (presents + loot) (days-1)

where loot = sum [1..days]

twelve_days = crimbo 0

— using map

f a b = (a + b) * ((1 + (b – a)) / 2)

tolv_dager days = sum $ map (f 1) [1..days]

*Main> twelve_days 12

364

*Main> tolv_dager 12

364.0

sum $ map (\x -> (1+x) * (x/2)) [1..12]

[…] The Twelve Days Of Christmas (programmingpraxis.com) […]

My Java solution here.

[…] The Twelve Days Of Christmas (programmingpraxis.com) […]

When run, this produces:

[…] The Twelve Days Of Christmas (programmingpraxis.com) […]

[…] The Twelve Days Of Christmas (programmingpraxis.com) […]