General mathematical solution to this problem? Code solution given I'd like to count the number of times a ball will bounce above a certain height. Imagine a ball dropped from a building, and which bounces by a factor of 0,66. How many times it will reach the 2nd floor window at 1.5 meters high from the floor?
I solved it using code, but I thought that maybe there is a general solution to this. Unfortunately my maths courses are far now and I can't quite remember the techniques.
Assumptions are:

*

*height > window's height

*1 > bounce rate > 0

Here is the code I used to solve it:
def number_of_bounces(height, bounce_rate, window_height):
    """
    Calculates the number of times the ball will pass in front of the window
    """
    if height < window_height:
        return 0
    next_bounce = height * bounce_rate
    if next_bounce < window_height:
        return 1

    passage = 1
    while next_bounce > window_height:
        passage += 2 # Once upward, once downward
        next_bounce = next_bounce * bounce_rate

    return passage

Any idea for a general solution to this problem?
Edit : Tests for this problem :
Initial height : 2 / Bounce rate : 0.5 / Windows height : 1 / Expected result : 1
Initial height : 3 / Bounce rate : 0.66 / Windows height : 1.5 / Expected result : 3
Initial height : 30 / Bounce rate : 0.66 / Windows height : 1.5 / Expected result : 1
Initial height : 30 / Bounce rate : 0.75 / Windows height : 1.5 / Expected result : 21
 A: Let $H$ be the initial height and $h$ the height of the window.
You are looking for the quantity $2n$ such that
$$H 0.66^{n+1}<h\le H 0.66^{n}$$
Taking logarithms, you will have
$$\ln(H)+(n+1) \ln(0.66) < \ln(h) \le \ln(H)+n \ln(0.66)$$
Now form $\frac{\ln(h)-\ln(H)}{\ln(0.66)}$. What can you conclude ?
A: We have by energy
$$
\cases{
mgh_0 = \frac 12 m v_0^2\\
mgh_1 = \frac 12 m v_1^2\\
\vdots\\
mgh_k = \frac 12 m v_k^2
}
$$
and also $v_{k+1} = \lambda v_k$ then
$$
h_{k+1} = \lambda^2 h_k,\ \ \ h_0 = H
$$
and solving this recurrence
$$
h_k = H\lambda^{2k}
$$
but $h_k = \frac g2 t_k^2$ or $t_k = \sqrt{\frac 2g H}\lambda^k$ and finally
$$
T_k = t_0+2\sum_{j=1}^{j=k} t_j
$$
$$
T_k = \sqrt{\frac 2g H}+2\sqrt{\frac 2g H}\sum_{j=1}^k\lambda^j = \sqrt{\frac 2g H}+2\lambda\sqrt{\frac 2g H}\left(\frac{1-\lambda^k}{1-\lambda}\right)
$$
NOTE
The "number of seen bounces" can be obtained as follows:
From $h_k = H\lambda^{2k}\ge \bar{h}$ we conclude that
$$
k =  \text{floor}\left[\frac 12\log_{\lambda}\left(\frac{\bar{h}}{H}\right)\right]+1
$$
here $\lambda\lt 1$
or
$$
k =  \text{floor}\left[\frac 12\ln\left(\frac{\bar{h}}{H}\right)/\ln(\lambda)\right]+1
$$
Attached a MATHEMATICA script showing the bouncing process.
parms = {H -> 3, h -> 1.5, mu -> 0.66, g -> 9.81};
parms = {H -> 30, h -> 1.5, mu -> 0.66, g -> 9.81};
parms = {H -> 30, h -> 1.5, mu -> 0.75, g -> 9.81};
tmax = 15;
ODE = {y''[t] == -g, y'[0] == 0, y[0] == H, WhenEvent[y[t] == 0, y'[t] -> -mu y'[t]]} /. parms
soly = NDSolve[ODE, y, {t, 0, tmax}][[1]];
Plot[{y[t] /. soly, (h /. parms)}, {t, 0, tmax}, PlotRange -> {0, H /. parms}]


A: The ball is dropped with null velocity at $t=0, z=h$.
The ball dynamics is given by
$$
\left \lbrace
\begin{array}{ccl}
v_z &=& - gt \\
z &=& -\frac12 gt^2 +h
\end{array}
\right.
$$
Simple computations show that the velocity at impact time
$t_1=\sqrt{2h/g}$ is
$
v_1^-=-\sqrt{2gh}
$.
The velocity just after bounce is
$v_1^+ = -\eta v_1^-$.
We know that for an object sent from the ground at positive vertical velocity $v$
the maximum height being reached is
$h_\mathrm{max}={v^2}/{2g}$.
Call $v_n$ the velocity at $n$th bounce
It holds
$v_n^+ = \eta^n \sqrt{2gh}$
which yields the maximum height of $\eta^{2n}\cdot h$
Let $h=30$ $\eta=0.75$, we have
$0.75^{2*5}\cdot 30 = 1.68$ and
$0.75^{2*6}\cdot 30 = 0.95$
This gives 10+1=11 crossings as shown in the Mathematica plot above. So the answer must be 11 and not 21 ?
