Asked to implement a solution in code to the following description: suppose that from a library
On day one, 1 book can be borrowed. Then on the next two days, 2 additional books can be borrowed hence after two days 3 books had been borrowed and after three days, 5 books are out.
On the next three subsequent days (four,five, six), 3 books can be borrowed. In summary: After four days, 8 books are out After five days, 11 books are out After six days, 14 books are out
This continues indefinitely After seven days(4 books to be borrowed),18 books are out After eight days, 22 books out After nice and ten, 26 and 30 books respectively are out
Is someone able to spot a formula (sum or other) to determine how many books can be borrowed for a given D = number of days?
Many thanks, Yaron
After $d$ days, where $$d=1+2+\dots+n+r=\frac{n(n+1)}2+r,\quad0\le r\le n,$$ the number of borrowed books is $$b_d=1^2+2^2+\dots+n^2+(n+1)r=\frac{n(n+1)(2n+1)}6+(n+1)r.$$
Given $d,$ the "rest" $r$ can be deduced from $n,$ which itself can be found as the index of the largest triangular number $\le d$: $$n=\left\lfloor\frac{\sqrt{8d+1}-1}2\right\rfloor.$$
As a check, here is a screenshot of the first values given by these formulas, on a spreadsheet of OpenOffice:
Note moreover that for large numbers $d$, these direct formulas are less calculation-consuming than iterative or recursive ones.