Happy PI day with Rust !
Machin’s formula, arbitrary-precision integers (ramp::Int):
fn machinpi(dcnt: usize) -> Int {
fn arccot(x: &Int, unity: &Int) -> Int {
fn _arccot(x: &Int, n: &Int, xpow: &Int, term: &Int, l: &Int) -> Int {
let zero = Int::from(0);
if zero == *term {
term.to_owned()
} else {
let one = Int::from(1);
let a = partial!(_arccot => x, &(n + 2), &(xpow / x), &(xpow / n), _);
let b = xpow / n;
if one == *l {
a(&zero) + b
} else {
a(&one) - b
}
}
}
_arccot(&(x * x), &Int::from(1), &(unity / x), &(unity / x), &Int::from(1))
}
let guard = 10 + 10_f32.log10().ceil() as usize;
let unity = Int::from(10).pow(dcnt + guard);
let a = 4 * arccot(&Int::from(5), &unity);
let b = arccot(&Int::from(239), &unity);
let c = &Int::from(10).pow(guard);
4 * (a - b) / c
}