/// The amount of iterations to perform when calculating the Newton-Raphson approximation.
const NEWTON_ITERATIONS: u64 = 32;
// Newton's method to approximate D
let mut d_prev: Uint512;
let mut d: Uint512 = sum_x.into();
for _ in 0..32 {  // Original Curve uses 256
    let mut d_prod = d;
    for amount in amount_times_coins.clone().into_iter() {
        d_prod = d_prod
            .checked_mul(d)
            .unwrap()
            .checked_div(amount.into())
            .unwrap();
    }
    d_prev = d;
    d = compute_next_d(amp_factor, d, d_prod, sum_x, n_coins).unwrap();
    // Equality with the precision of 1
    if d > d_prev {
        if d.checked_sub(d_prev).unwrap() <= Uint512::one() {
            break;
        }
    } else if d_prev.checked_sub(d).unwrap() <= Uint512::one() {
        break;
    }
}
let mut y_prev: Uint512;
let mut y = d;
for _ in 0..32 {  // Original uses more iterations
    y_prev = y;
    let y_numerator = y.checked_mul(y).unwrap().checked_add(c).unwrap();
    let y_denominator = y
        .checked_mul(Uint512::from(2u8))
        .unwrap()
        .checked_add(b)
        .unwrap()
        .checked_sub(d)
        .unwrap();
    y = y_numerator.checked_div(y_denominator).unwrap();
    // Check convergence
    if |y - y_prev| <= 1 break;
}
    for _i in range(255):
        D_P: uint256 = D
        for _x in _xp:
            D_P = D_P * D / (_x * N_COINS)  # If division by 0, this will be borked: only withdrawal will work. And that is good
        Dprev = D
        D = (Ann * S / A_PRECISION + D_P * N_COINS) * D / ((Ann - A_PRECISION) * D / A_PRECISION + (N_COINS + 1) * D_P)
        # Equality with the precision of 1
        if D > Dprev:
            if D - Dprev <= 1:
                return D
        else:
            if Dprev - D <= 1:
                return D
    # convergence typically occurs in 4 rounds or less, this should be unreachable!
    # if it does happen the pool is borked and LPs can withdraw via `remove_liquidity`
    raise
