# Holiday challenge!

Our team participated in a remote trivia event this week, of course using Coda docs to collaborate on answers.

One of the questions referred to the song “The Twelve Days of Christmas”. If you haven’t heard it, the lyrics run through a list of gifts each day. (Full lyrics here.) The question asked: How many gifts were given in this song?

1
1 + 2
1 + 2 + 3
1 + 2 + 3 + 4

Until the final day: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12

Our team came up with some fun formulas to get to the answer, so I thought it would be a fun challenge to pose here. Let us know what you come up!

6 Likes

Sequence(1,12).FormulaMap(Sequence(1,CurrentValue).Sum()).Sum()

2 Likes

Hint: Gauss figured this out in elementary school (sort of)
Other hint: It’s also related to the number of edges in a fully connected graph
Edit: I was wrong. It’s the triangular numbers, not the number of edges in a fully connected graph

3 Likes

Did I get it right?
Another way:

There are 12 * 1, 11 * 2, 10 * 3…
Σ(1,12)[13-n]*n
Sequence(1,12).FormulaMap((13-CurrentValue)*CurrentValue).Sum()

3 Likes

As Paul said before deleting their post (not sure why?), there is also a way to do it without `FormulaMap`

Also, @Breno_Nunes, it might be nice to blur your answers to save people from the spoilers in case they want to take a shot. You can do that like this:

3 Likes

I didn’t even know it was possible. Thank you.

1 Like

I got confused at first. To simply calculate the sum of 1 + 2 + … + N, there’s an arithmetic progression sum formula `(1 + N) * (N / 2)` (because 1+13 + 2+11 + 3+10… i.e. 13 x half the numbers.

But then I saw that I had to sum the progressions themselves as well. So this formula isn’t really relevant here.

I still have a feeling that it can be simplified to a polynomial that doesn’t have to use loops

There is indeed a polynomial expansion: see link.

But the loops method is fun. Anyone use `WithName()` to do it?

3 Likes

Ha cool, knew it! The tetrahedra explanation is awesome.

Loops is fun, but a simple polynomial is efficient and I favor efficient.

2 Likes

One way to do it using `WithName`:

``````WithName(Sequence(1,12), Twelve,
Twelve.FormulaMap(
WithName(CurrentValue, CurrentDay,
Twelve.Slice(1, CurrentDay).Sum()
)
)
).Sum()
``````

I guess this is similar to @Breno_Nunes’ first solution, except `WithName` allows us to reuse the initial array and take slices of it, instead of making a new one.

One of the nice things about `WithName` is it helps to make formulas a bit more self-documenting

1 Like