1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 19:19:07 +01:00
CodeWars/5kyu/which_x_for_that_sum/solution.rs

25 lines
569 B
Rust
Raw Normal View History

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,
}
}