1

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.

11
  • If it's for studying purpose then cool, go on with it. if you just need PI in your program use Math.PI. Commented Nov 29, 2015 at 10:28
  • 1
    ans=ans.multiply(a); at the end of your loop shouldn't be there. That math should be done after the summation is completed. Commented Nov 29, 2015 at 10:38
  • ug_ yes, I know that was there from when I was debugging. Commented Nov 29, 2015 at 10:47
  • guillaume thanks for pointing that out however, it still doesn't work Commented Nov 29, 2015 at 10:50
  • I Try various k, and result doesnt converge. Perhaps modify your code to show each 10 (or 100) steps. Somewhere, there is perhaps some limitation inside variables ... Commented Nov 29, 2015 at 11:08

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

Haha I had forgotten that Thanks!
@Paul You can help reduce problems like this from happening making some new habits in your coding. Try to declare variables only before they will be used and in the smallest scope possible. In this case declare a and root2 right before you apply them. Also using an IDE will help out alot with keeping your code clean and finding bugs. For instance when I put this code in my Eclipse it quickly underlined the variable a telling me it was unused.
Thanks, I was actually using eclipse to code this but I didn't have that option on apparently. Also, thanks for the tips, I will try my best to implement them :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.