# Daily Challenge #267 - Braking Speed

Daily Challenge (270 Part Series)

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.

1. Calculate the total stopping distance in meters given v, mu (and the reaction time t = 1).
dist(v, mu) -> d = total stopping distance

2. Calculate v in km per hour knowing d in meters and mu. The reaction time is still t = 1.
speed(d, mu) -> v such that dist(v, mu) = d

### Examples

1. dist(100, 0.7) -> 83.9598760937531

2. 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!

Daily Challenge (270 Part Series)

Posted on by:

### dev.to staff

The hardworking team behind dev.to ❤️

### Discussion

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.

Some may get different result, it does not matter if you get 10 metres with one method and 12.5 metres with another – both are obviously closest to 10, which is thus the right answer.

# Calculate for Stopping Distance:

First we calculate the reaction distance by using this equation:

Actually, this is classic high school physics, but let's see the equation

$rd = {v \times 1 \over 3.6}$

Second we calculate the breaking distance using the reaction distance:

$bd = {rd^2 \over 2 \times mu \times 9.81}$

Now both distances are combined to get stopping distance:

$d=rd+bd$

# Calculate for Average Speed:

Remember that we get the average speed by using equation below:

$s = {distance \over time}$

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$ :

What will matter here is that we get the velocity from the reaction distance

$d = rd+bd$

$rd = d-{v^2 \over 2 \times mu \times 9.81}$

$\sqrt[2]{-\lparen 2 \times mu \times 9.81\rparen rd - d} = v$

$v = \sqrt[2]{-\lparen 2 \times mu \times 9.81\rparen rd - d}$

Now, let's convert this into code:

Calculation for stopping distance:

/* 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

Calculation for average speed:

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
/* Then average speed by rd = v / 3.6 to  v = rd x 3.6 */
let s_ = d_ * 3.6

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.

dist :: Double -> Double -> Double
dist vKmh mu = d_reaction + d_braking
where
d_reaction = v*t_reaction
d_braking = (v*v) / (2*mu*g)
v = fromKmh vKmh

speed :: Double -> Double -> Double
speed d mu = toKmh v
where
v = (-b + (sqrt \$ b*b - 4*a*c)) / (2*a)
a = 1 / (2*mu*g)
b = 1 / t_reaction
c = -d

fromKmh :: Double -> Double
fromKmh v = v * 1000 / 3600

toKmh :: Double -> Double
toKmh v = v * 3600 / 1000

g :: Double
g = 9.81

t_reaction :: Double
t_reaction = 1

Tests:

> dist 144 0.3
311.83146449201496

> dist 92 0.5
92.12909477605366

> speed 159 0.8
153.79671564846308

> speed 153 0.7
142.14404997566152

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.

const g = 9.81; // gravity in m/s
const k = 3600 / 1000; // conversion coefficient: m/s to km/hr

const dist = (v, mu, t=1) => {
v = v / k;
return v * t + v * v / (mu * 2 * g);
}

const speed = (d, mu, t=1) => {
return k * (Math.sqrt(g * mu * (2 * d + g * t * t * mu)) - g * t * mu);
}

console.log(dist(144, 0.3)) // 311.83146449201496
console.log(dist(92, 0.5)) // 92.12909477605365
console.log(speed(159, 0.8)) // 153.79671564846308
console.log(speed(153, 0.7)) // 142.14404997566152