-1

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

2 Answers2

2

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:

enter image description here

Note moreover that for large numbers $d$, these direct formulas are less calculation-consuming than iterative or recursive ones.

Anne Bauval
  • 49,005
  • 2
    I saw this by accident ( I was not notified , even with the @Prem ) , I just wanted you to know that I rarely Downvote & I never Downvote when typo/error can be Corrected via Commenting !! – Prem Jun 22 '23 at 11:01
  • 2
    Some "Drive-By" Downvoter has Downvoted & left & might likely never come back here. I will try to rectify that with my Upvote now !! – Prem Jun 22 '23 at 11:29
  • 2
    Even though the other Answer is wrong , I have not Downvoted that , I have just left a Comment to take action , to make Corrections. – Prem Jun 22 '23 at 11:43
  • 1
    @Prem Thank you for your positive attitude, I am not so good at that, I shall try to remember and imitate. – Anne Bauval Jun 22 '23 at 11:45
  • Thank you all for the valuable insight. With this in mind - I have been looking and considering the calculation of 'r' / 'rest' . Suppose my program starts and the first input days' query I receive is d = 11. Using the formula for calculating n from d --> I can get that n= 4 (thank you @AnneBauval , already done so) but then I look at the value of r , r = 1 so it is not the first row for which n = 4 , however I do not yet know this. For a randomly given d , am I correct in stating that the program will need to perform a few previous d's calculations to find the correct value of r. – yarong76 Jun 22 '23 at 19:26
  • This will the case when the value of n=4 drops down to n=3 (for d = 9, where immediately I can then say that for d=9-->r=3). Since I performed 2 additional calculations, I then know that for d=11 (original problem), r = 1). Performing the additional calculations is for sure a benefit , as the program can cache previously requested d's (so if user requests result of d = 10, I will have performed it already). Again, I now understand 0 <= r <= n , we can not know directly the value of r directly from d - unless I am wrong ? :) – yarong76 Jun 22 '23 at 19:30
  • No "additional calculations" are needed, the calculation of $n, r,b$ from $d$ is direct (this is the main advantage of this answer over @Piita's):$$n:=\left\lfloor\frac{\sqrt{8d+1}-1}2\right\rfloor,\quad r:=d-\frac{n(n+1)}2,$$$$b:=\frac{n(n+1)(2n+1)}6+(n+1)r,$$ nothing more. You can even squeeze the calculation of $r:$ $$b=(n+1)\left(d-\frac{n(n+2)}6\right).$$ – Anne Bauval Jun 22 '23 at 21:46
  • I don't know if "squeeze" is the right word. I meant you can bypass that step. – Anne Bauval Jun 22 '23 at 21:57
1

One formula is, for the number of books $b$ and the number of a given day $D$:

$$b=\sum_{k=1}^D\left\lfloor\frac{1}{2}+\sqrt{2k}\right\rfloor,$$
where $(\lfloor\cdot\rfloor)$ is the floor function and $(\sum_{k=1}^D\cdot)$ is the summation of $D$ terms.

How to read this:

For day $D=1$, we calculate one term, namely $(\frac{1}{2}+\sqrt2)\approx 1.9$ and then round this down to the closest integer, in this case $b_{D=1}=1$.

For day $D=2$, we calculate two terms, namely again $(\frac{1}{2}+\sqrt2)\approx 1.9$ which we round down to $1$, and also $(\frac{1}{2}+\sqrt4)=2.5$ and then round this down to the closest integer, in this case $2$, to add them together. So, $b_{D=2}=1+2=3$.

Anne Bauval
  • 49,005
Piita
  • 975
  • O right - is it a known description of a similar problem ? This term generator is really nice (I had to look at it a number of times…as if I needed proof that I am currently jet-lagged after multiple flights across timezones ) – yarong76 Jun 22 '23 at 11:16
  • 1
    You are not using the INDEX variable , hence that formula is wrong. It might require tweaking. – Prem Jun 22 '23 at 11:26
  • Is this something that can be proven by induction ? I have a few test cases for a number of input Days , but I am in the middle of traveling today so can’t implement it in code (May try tonight ). What does the INDEX variable denote ? – yarong76 Jun 22 '23 at 12:03
  • It is the variable in the sum. Piita surely meant $b=\sum_{k=1}^D\lfloor\frac{1}{2}+\sqrt{2k}\rfloor.$ I checked the first values (same as mine) but (contrarily to my formula) I don't know how (s)he proves that one. – Anne Bauval Jun 22 '23 at 12:27
  • 1
    Yes , that is Correct , that is listed in the OEIS ( which I gave in the Comment to the Question Post ) Entry A060432 [[ PROG (PARI) f(n) = floor(1/2+sqrt(2*n)) .... ]] .... [[ FORMULA Let f(n) = floor(1/2 + sqrt(2*n)), then .... ]] , @AnneBauval , though Proof is not given. – Prem Jun 22 '23 at 15:16
  • 2
    Correct @Prem, fixed now, ty. – Piita Jun 22 '23 at 15:23