def tetrate(a, tetrate_acc):
assert is_valid_other(a), "not a valid other"
assert is_valid_accumulator(tetrate_acc), "not a valid accumulator"
assert a > 0, "we don't define 0↑↑b"
exponentiate_acc = xmpz(1)
for _ in count_down(tetrate_acc):
multiply_acc = xmpz(1)
for _ in count_down(exponentiate_acc):
add_acc = xmpz(0)
for _ in count_down(multiply_acc):
for _ in range(a):
add_acc += 1
multiply_acc = add_acc
exponentiate_acc = multiply_acc
return exponentiate_acc
And here is the final function in Rust:
(I used `&mut` instead of returned values because I don't think Rust guarantees that returned owned values aren't copied.)
fn tetrate(a: u32, acc: &mut BigUint) {
assert!(a > 0, "we don’t define 0↑↑b");
let mut exp = BigUint::from(1u32);
for () in acc.count_down() {
let mut mul = BigUint::from(1u32);
for () in exp.count_down() {
let mut add = BigUint::ZERO;
for () in mul.count_down() {
for _ in 0..a {
add += 1u32;
}
}
mul = add;
}
exp = mul;
}
*acc = exp;
}