Search code examples
javaloopsfor-loopincrementpi

Write a program to estimate PI (π) using the Leibniz series in Java


I've looked online for hours trying to see if I could find a solution and while I have found many solutions my instructions from my professor are as follows:

Write a program to estimate PI (π) using the following series. This problem is also described in the text as problem 5.25 at the end of chapter 5. If you are unfamiliar with series, problem 5.24 is a single pass through a series and the solution is posted in the Homework 3 course module.
π=4*(1-1/3+1/5-1/7+1/9-1/11+⋯〖-1〗^(i+1)/(2i-1)) Obviously, there is no user input for this problem so a modified worksheet is provided. You will to write a program that computes PI using i values of 10,000 to 100,000 in increments of 10000. Your output should look like: (Hint: Placing “/t” in the System.out.println between the values of i and PI will give you columns. This is the tab character).

i________PI

10000____3.xxxxx

20000____3.xxxx

30000____3.xxxx

You will need multiple loops to do this. The outer loop will increment i. The inner loop will compute the series from 1 to i. You may use any of the three types of loops, for, while, or do-while to do this.

Now I am well aware that there are many ways that are better at finding pi besides this, however the point of this assignment is not to find Pi efficiently but rather to practice with loops. However I've tried numerous approaches and all of them return either infinite loops, incorrect outputs, or they just don't compile.

EDIT: Thanks to Martijn Courteaux I've made a vast improvement in the code. however I still can't seem to get the count to increment properly. Any Suggestions? My Most recent attempt is as follows.

public class LeibnizFormula { public static void main(String[] args) {

 System.out.println("i/t Pi");
  int count = 10000;
  double pi = 0;
  double denominator = 1;
  while(count < 100000){ 
      for (int x = 0; x < count; x++) {

        if (x % 2 == 0) {
          pi = pi + (1 / denominator);
        } 
        else {
          pi = pi - (1 / denominator);
        }
        denominator = denominator + 2;
      }
      pi = pi * 4;
      System.out.println(pi);
      count = count + 10000;
      System.out.println(count);
  }
}
}

Now the results are:

i/t Pi
3.1414926535900345
20000
12.566037281026608
30000
50.264165790773355
40000
201.05666982975973
50000
804.2266826523694
60000
3216.9067325142446
70000
12867.626931247545
80000
51470.50772578291
90000
205882.03090368543
100000

I don't really understand why the program is works when I hardcode the values of "count" in but not when increment it. I know I'm sounding tedious but i really want to understand what is going wrong and WHY it's going wrong.

I realize that it's not good form to simply post the solution here so I'm not asking for that, I just need possibly some pseudo code or pointers. I've been working on this for quite a while. Thank you


Solution

  • There are more efficient and concise ways of writing this program, but the easiest fix would be to get rid of the pi = pi * 4, because then the next iteration of the while loop will have a much larger value of pi to start. Instead, simply print out pi * 4 instead of just pi.

    Also, this may not matter but you are actually giving more precision than is needed; for example, for count = 20000 the precision is actually what count = 30000 should be. Your inner for loop has x < count, and since count increases by 10000 after each iteration, you are really getting precision at 10000, 30000, 60000, etc. You can fix this by either reinitializing the values of denominator and pi each iteration, or changing x < count to x < 10000.