|
|
@ -31,28 +31,23 @@ impl Continuous { |
|
|
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#
|
|
|
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#
|
|
|
// Continued_fraction_expansion
|
|
|
// Continued_fraction_expansion
|
|
|
pub fn sqrt(x :i64, a0 :i64) -> Self {
|
|
|
pub fn sqrt(x :i64, a0 :i64) -> Self {
|
|
|
fn inner(mut v :Vec<i64>,
|
|
|
|
|
|
x :i64,
|
|
|
|
|
|
a0 :i64,
|
|
|
|
|
|
mn :i64,
|
|
|
|
|
|
dn :i64,
|
|
|
|
|
|
an :i64) -> Vec<i64> {
|
|
|
|
|
|
|
|
|
fn inner(v :&mut [i64], x :i64, a0 :i64, mn :i64, dn :i64, an :i64) {
|
|
|
let mn_1 = dn * an - mn;
|
|
|
let mn_1 = dn * an - mn;
|
|
|
let dn_1 = (x - mn_1 * mn_1) / dn;
|
|
|
let dn_1 = (x - mn_1 * mn_1) / dn;
|
|
|
let an_1 = (a0 + mn_1) / dn_1;
|
|
|
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
|
|
|
// The convergence criteria „an_1 == 2 * a0“ is not good for
|
|
|
// very small x thus I decided to break the iteration at constant
|
|
|
// very small x thus I decided to break the iteration at constant
|
|
|
// time. Which is the 10 below.
|
|
|
// 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<i64> = vec!(0; 10);
|
|
|
|
|
|
inner(&mut v, x, a0, 0, 1, a0);
|
|
|
|
|
|
Continuous(v)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|