1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-16 20:56:57 +02:00
CodeWars/6kyu/parabolic_arc_length/solution.rs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

20 lines
450 B
Rust

fn len_curve_of<F>(f: F, from: f64, to: f64, n: i32) -> f64
where
F: Fn(f64) -> f64,
{
let dx = (to - from) / n as f64;
let dx2 = dx * dx;
(0..n)
.map(|i| {
let left = f(from + i as f64 * dx);
let right = f(from + (i + 1) as f64 * dx);
(dx2 + (left - right).abs().powf(2.0)).sqrt()
})
.sum()
}
pub fn len_curve(n: i32) -> f64 {
len_curve_of(|x| x * x, 0.0, 1.0, n)
}