From 8312383ffa0ea0290f2911fcba0aa3b62f5046bf Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 15 Dec 2019 23:54:29 +0100 Subject: [PATCH] continuous sqrt now uses a slice internally --- fractional/src/continuous.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/fractional/src/continuous.rs b/fractional/src/continuous.rs index 361c84d..13fc3d5 100644 --- a/fractional/src/continuous.rs +++ b/fractional/src/continuous.rs @@ -31,28 +31,23 @@ impl Continuous { // https://en.wikipedia.org/wiki/Methods_of_computing_square_roots# // Continued_fraction_expansion pub fn sqrt(x :i64, a0 :i64) -> Self { - fn inner(mut v :Vec, - x :i64, - a0 :i64, - mn :i64, - dn :i64, - an :i64) -> Vec { + fn inner(v :&mut [i64], x :i64, a0 :i64, mn :i64, dn :i64, an :i64) { let mn_1 = dn * an - mn; let dn_1 = (x - mn_1 * mn_1) / dn; let an_1 = (a0 + mn_1) / dn_1; - v.push(an); - + v[0] = an; // The convergence criteria „an_1 == 2 * a0“ is not good for // very small x thus I decided to break the iteration at constant // time. Which is the 10 below. - match v.len() { - 10 => v, - _ => inner(v, x, a0, mn_1, dn_1, an_1), + if v.len() > 1 { + inner(&mut v[1..], x, a0, mn_1, dn_1, an_1); } } - Continuous(inner(Vec::new(), x, a0, 0, 1, a0)) + let mut v :Vec = vec!(0; 10); + inner(&mut v, x, a0, 0, 1, a0); + Continuous(v) } }