# My code works, But how do I make this code run in a deterministic time?

The Problem: Given 3 inputs Bounce, Ball drop height, and ball view height.

How do I calculate the number of times the observer can see the ball pass. So my code gives correct output, but it takes a longer time as bounce approaches 1, how do I make it deterministic in nature.

def bouncingBall(h, bounce, window):
BounceFactor = bounce
BallDropHeight = h
ViewerHeight = window
BounceLeft = h
BallSeenTimes = 1
if bounce > 1 or bounce < 0 or window >= h or h ==0:

return -1
else:
while (BounceLeft > ViewerHeight):
BounceLeft = BounceLeft * BounceFactor
if (BounceLeft > ViewerHeight):
BallSeenTimes = BallSeenTimes + 2
else:
break
return BallSeenTimes


I am not looking for code answers, just the direction in which I need to think. Currently, I think If I could somehow establish a linear relationship and create a function to "guess" the passes it would be faster.

• Posted at Stackoverflow at same time. Please do not post the same question on multiple sites. Each community should have an honest shot at answering without anybody's time being wasted. If you don't get a satisfying answer after a week or so, you may flag to request migration. I am voting for closure since there have been several comments to the other posts. – Apass.Jack Nov 14 '18 at 16:50
• Duh, I should have searched for existing answers. I just retracted my vote and recommended the other post be deleted. – Apass.Jack Nov 14 '18 at 17:27

Assuming I've understood the problem correctly, you're dropping a ball from height h and, when dropped from height x, it bounces to height x*Bouncefactor. You want to know how many times it passes through the height you're calling window. Your approach is, essentially, to simulate the bouncing ball and count.
Instead of doing that, you should just calculate how many times the ball bounces before it's at height less than window. This is the least k such that h * Bouncefactor^k < window, so you just need to solve this equation and then convert the number of bounces into the number of times that the ball is seen.