TADM2E 2.34

From Algorithm Wiki
Jump to: navigation, search

This task refers to this song The_Twelve_Days_of_Christmas_(song)

So we have to calculate the sum like this

1 day - 1 gift

2 day - 1 gift + 2 gifts

3 day - 1 + 2 + 3

n day - 1 + 2 + 3 + ... + n


Formula is

$ \begin{align} &\sum_{i=1}^n \sum_{j=1}^n j\ = \sum_{i=1}^n \frac{n(n+1)}{2} =\\ &= \frac{1}{2}\sum_{i=1}^n n^2 + \frac{1}{2}\sum_{i=1}^n n=\\ &= \frac{n(n+1)(2n+1) + 3n(n+1)}{12} = \frac{(n+1)(n^2+2n)}{6}\ \end{align} $