1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-22 08:33:48 +01:00
CodeWars/5kyu/which_x_for_that_sum/solution.rs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

24 lines
569 B
Rust

fn solve_quadratic(a: f64, b: f64, c: f64) -> Option<(f64, f64)> {
let d = b * b - 4.0 * a * c;
if d < 0.0 {
None
} else {
Some(((-b + d.sqrt()) / (2.0 * a), (-b - d.sqrt()) / (2.0 * a)))
}
}
pub fn solve(m: f64) -> f64 {
match solve_quadratic(m, -2.0 * m - 1.0, m) {
Some((left, right)) => {
if left > 0.0 && left < 1.0 {
left
} else if right > 0.0 && right < 1.0 {
right
} else {
0.0
}
}
None => 0.0,
}
}