DEV Community

Discussion on: Daily Challenge #267 - Braking Speed

Collapse
 
cipharius profile image
Valts Liepiņš

First part of the challenge is almost written out as is, but the second part requires solving quadratic equation.

Solution in Haskell:

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
    -- Solve using quadratic formula
    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