Search code examples
javaalgorithmbigdecimalpi

Implementing Pi algorithm in java


I have searched the web for an algorithm that could calculate the value of pi in an efficient manner and I found this:

enter image description here

Using a calculator, I was able to calculate the first few digits of pi by hand. Then I knew that the formula works so I tried to implement it However, the output was no where near the value of pi. I then tried printing the value of k and the output every time I incremented k and found that the output was going from 3 to 10 then to 1 and so on. It would be appreciated if someone could point out the flaw in my program.

Here is my code:

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

  public class pi2 {
static MathContext mc = new MathContext(1000, RoundingMode.HALF_EVEN);

public static void main(String[] args) {

    BigDecimal root2=new BigDecimal("1.41421356237309504880168872420969807856967187537694807317667973799");
    BigDecimal a=(root2.multiply(new BigDecimal("2"))).divide(new BigDecimal("9801"),mc);
     BigDecimal ans=new BigDecimal("0");
    for(BigDecimal k=new BigDecimal("0");k.compareTo(new BigDecimal("2000"))<=0;k=k.add(new BigDecimal("1"))){


    BigDecimal num=fact(k.multiply(new BigDecimal("4")));
    num=num.multiply(new BigDecimal("1103").add(k.multiply(new BigDecimal("26390"))));
    BigDecimal den=fact(k).pow(4);
            den=den.multiply(new BigDecimal("396").pow(k.multiply(new BigDecimal("4")).intValueExact()));
    ans=ans.add(num.divide(den,mc));



    }
    ans=new BigDecimal("1").divide(ans,mc);
    System.out.println(ans);

}
public static BigDecimal fact(BigDecimal n){
    BigDecimal fact=new BigDecimal("1");
    for(BigDecimal x=new BigDecimal("2");n.compareTo(x)>=0;x=x.add(new BigDecimal("1"))){
        fact=fact.multiply(x);
    }
    return fact;

}

 }

Here is the output:

5.580282058008853402510559291287581867553431390145503640210100548605034743358097050034424720628698860929750569800058429421656020670208288633526864302454613600847940382311736134643671764886381747697190968564014260705233085812484714762274969350204069098619933375627715134891912898782601108729886146330332238253003691746047262799485039571747683663565532369064391166325006674455252331737698907670644446295854092826000968263741742613071668128325081312284036131088326091218220137819968277353340599533207728105864448598414581380099885134601317006991806489649924935353370069906252625046822796244633763704419705976717286963549896200415922555506633154441005278818242772512067320640614989708237011539020994102009040721602261875679359630595715795837939333694056692953617077290030676459281218578044754236976994457097401172359470109498529296892483489384488461202901916083392300861779680090077181505893710321463758029409577389297666918447580531492394294312022253607251169070143541696305881176305678794312168269627285E+15966

Thanks for your help.


Solution

  • You aren't applying the 2*sqrt(2)/9801 (a) at all to your equation. Essentially the variable a is not used right now.

    Simply change the end of main method to read:

    // i moved a down here because its not used above this location.
    BigDecimal a = (root2.multiply(new BigDecimal("2"))).divide(new BigDecimal("9801"), mc);
    ans = new BigDecimal("1").divide(ans.multiply(a), mc);
    System.out.println(ans);
    

    Running this on my machine prints out 3.141592