Calculating pi to 1000 digit

Can anyone help me with writing a function to calculate pi to a specific digit?
like pi(1000) and I get pi to 1000 digits?

I already searched for it but couldn't find it.

Are you just looking for the algorithm, or an example in Rust?

DDG's first hit for "calculate pi pseudocode" brings some decent answers on this StackExchange question.

The top one is a straight-forward algorithm, and then there's a link to the Chudnovsky algorithm which is apparently quite fast.

A rough example in Rust was pretty easy to DDG, as well.

Now, I haven't personally tried 'em but hopefully that's more material to help you with what you're looking for.

/// PI = 16 * atan(1/5) - 4 * atan(1/239)
fn pi (precision: u64) -> String
{
    /// atan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9...
    fn atan (x: Fraction, precision: u64) -> Fraction
    {
        use ::num_traits::pow::Pow;
        let end: BigUint =
            BigUint::from(10_u32)
                .pow(precision)
        ;
        let target = Fraction::new(1.into(), end);

        let mut current_term = x.clone();
        let mut ret = Fraction::from(0);
        let mut sign = BigInt::from(1);
        let mut n = BigUint::from(1_u32);
        let mut x_pow_n = x.clone();
        let two = BigUint::from(2_u32);
        let x_square = &x * &x;

        while current_term.abs() > target {
            ret = ret + current_term;
            // eprintln!(
            //     "atan({}) ~ {}",
            //     x,
            //     ret.decimal(precision as usize),
            // );
            n += &two;
            sign = -sign;
            x_pow_n = x_pow_n * &x_square;
            current_term = &x_pow_n * Fraction::new(
                sign.clone(),
                n.clone(),
            );
        }
        ret
    }

    let precision_usize = usize::
        try_from(precision)
            .expect("Overflow")
    ;
    let pi_approx = Fraction::sub(
        Fraction::from(16) * atan(
            Fraction::new(1.into(), 5_u32.into()),
            precision
                .checked_add(2) // 16 -> 10 ^ 2
                .expect("Overflow"),
        ),
        Fraction::from(4) * atan(
            Fraction::new(1.into(), 239_u32.into()),
            precision + 1, // 4 -> 10 ^ 1
        ),
    );
    pi_approx.decimal(precision_usize)
}
4 Likes

thanks a lot