Braking distance d1
is the distance a vehicle will go from the point when it brakes to when it comes to a complete stop. It depends on the original speed v
and on the coefficient of friction mu
between the tires and the road surface.
Braking distance is just one of two principal components of the total stopping distance. The other component is the reaction distance, which is the product of the speed and the perception-reaction time of the driver. We can just assume that the reaction time is constant and equal to 1 second.
The kinetic energy E
can be found using the formula 0.5*m*v**2
, the work W
given by braking is mu*m*g*d1
. Finding E
and W
gives the braking distance: d1 = v*v / 2*mu*g
where g
is the gravity of Earth and m
the vehicle's mass.
So, to complete this challenge, there are two tasks you need to complete.
Calculate the total stopping distance in meters given
v
,mu
(and the reaction timet = 1
).
dist(v, mu) -> d
= total stopping distanceCalculate
v
inkm
per hour knowingd
in meters andmu
. The reaction time is stillt = 1
.
speed(d, mu) -> v such that dist(v, mu) = d
Examples
dist(100, 0.7) -> 83.9598760937531
speed(83.9598760937531, 0.7) -> 100.0
Tests
dist(144, 0.3)
dist(92, 0.5)
speed(159, 0.8)
speed(153, 0.7)
Good luck!
This challenge comes from g964 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Discussion (3)
Let's explain this of course of how we get the answer. In theory, there will be no absolute answer for this one. We only know the answer using floating-point approximation to returns the smallest interval between two representable numbers by Math.abs(answer - expect) <= 1e-2. I think we can also use Number.EPSILON.
Calculate for Stopping Distance:
First we calculate the reaction distance by using this equation:
Second we calculate the breaking distance using the reaction distance:
Now both distances are combined to get stopping distance:
Calculate for Average Speed:
Remember that we get the average speed by using equation below:
The question is what is distance and what is time. We can only reverse engineer the equation we did from earlier by getting the velocity from reaction distance equation.
First we will decompose the equation d=rd+bd to rd=d−bd to bd=d−rd :
Now, let's convert this into code:
Calculation for stopping distance:
Calculation for average speed:
Execution Experiment:
function dist(v, mu) { /* First we calculate the reaction distance: */ let rd = (v * 1) / 3.6 /* Then we calculate the braking distance: */ let bd = rd * rd / (2 * mu * 9.81) /* Now both distances are combined: */ let d = bd + rd /* Then return the value */ return d } function speed(d, mu) { let g = 9.81; /* First decompose stopping distance bd = rd - 1 to rd = bd - 1 */ let d_ = (-(2 * mu * g) + Math.sqrt((2 * mu * g) * (2 * mu * g) - (4 * -d * (2 * mu * g)))) / 2 /* Now average speed by rd = v / 3.6 to v = rd x 3.6 */ let s_ = Number(d_ * 3.6).toFixed(1) /* Then return the value */ return s_ } console.log(dist(144, 0.3)) // 311.83146449201496 console.log(dist(92, 0.5)) // 92.12909477605365 console.log(speed(159, 0.8)) // 153.8 console.log(speed(153, 0.7)) // 142.1
And, here's for Sheldon Cooper (☕):

First part of the challenge is almost written out as is, but the second part requires solving quadratic equation.
Solution in Haskell:
Tests:
Is there something about this problem I'm missing? This seems like it's just a math problem, is there some trick I'm missing that makes it faster? Or not lose precision? Or am I missing an edge case some how?
anyways, here's a javascript solution.