Million elements of PI in Java BBP formula

812 views Asked by At

I try to implement BBP formula for calculating milion elements of PI. I have problem with calculating and displaying results. When I set i for 1 000 000 in loop and MathContext() for more than 1000 than program not ending. I paste my code below, please help.

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Rsi {
    public static void main(String[] args) {
        BigDecimal iterationResult, temp, temp2, temp3, temp4, temp5, 
            one, two, four, five, six, eight, sixteen, ii, result, pd, tc;
        result = new BigDecimal(0);
        result.setScale(1000000, RoundingMode.HALF_DOWN);

        for (int i = 0; i < 1000000; i++) {
            pd = new BigDecimal(0);
            tc = new BigDecimal(0);

            temp = new BigDecimal(0);
            temp2 = new BigDecimal(0);
            temp3 = new BigDecimal(0);
            temp4 = new BigDecimal(0);
            temp5 = new BigDecimal(0);
            two = new BigDecimal(2);
            four = new BigDecimal(4);
            five = new BigDecimal(5);
            six = new BigDecimal(6);
            eight = new BigDecimal(8);
            sixteen = new BigDecimal(16);
            one = new BigDecimal(1);
            iterationResult = new BigDecimal(0);
            ii = new BigDecimal(i);

            // calculate 1/16^i

            temp = one.divide((sixteen.pow(i)), new MathContext(10,
                    RoundingMode.HALF_DOWN));

            // calculate 4/(8*i+1) first word
            temp2 = four.divide((eight.multiply(ii)).add(one), new MathContext(
                    1000, RoundingMode.HALF_DOWN));

            // calculate 2/(8*i+4) second word
            temp3 = two.divide((eight.multiply(ii)).add(four), new MathContext(
                    1000, RoundingMode.HALF_DOWN));

            // calculate 1/(8*i+5) third word
            temp4 = one.divide(((eight.multiply(ii)).add(five)),
                    new MathContext(1000, RoundingMode.HALF_DOWN));

            // calculate 1/(8*i+6) fourth word
            temp5 = one.divide(((eight.multiply(ii)).add(six)),
                    new MathContext(1000, RoundingMode.HALF_DOWN));

            // calculate ( 4/(8*i+1) )-( 2/(8*i+4) ) first - second
            pd = temp2.subtract(temp3);

            // calculate -third-fourth word
            tc = (temp4.negate()).subtract(temp5);

            // calculate 1/16^k * ((first - second) + (-third - fourth))
            iterationResult = temp.multiply((pd.add(tc)));

            result = result.add(iterationResult);

        }
        System.out.println("Result is " + result);
    }
}

Why i can't display 1 million of digits when i set loop for 1 million and MathContext for million too?

1

There are 1 answers

0
Tagir Valeev On

The problem is just your computer is too slow to compute it. The difficulty of this task grows very quickly (probably by n^3), so if it takes around 1 second to compute 1 000 digits, it will take roughly a billion seconds to compute 1 000 000 digits. Apparently such tasks are computed using heavily optimized code and using computational clusters. I guess, the BBP formula would work much better if you use specially crafted data type which performs all the intermediate computations in hexadecimal and convert to decimal only at the end to print the result. This way 1/16^i step will be trivial. So BigDecimal java type is not very optimal for such computations.