Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Computing Pi!

Options
  • 28-10-2013 6:29pm
    #1
    Registered Users Posts: 189 ✭✭


    Hi. We have been asked to compute Pi with the formula:

    PI = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + ...
    Allowing the user to determine the amount of computations to perform(5 shown in the above formula)

    I'm completely stumped at this stage and cannot get the correct answer no matter what I try. Can anyone give me a pointer as to what I should try and alter?
    import javax.swing.JOptionPane;
    
    import java.util.Scanner;
    
    public class pi 
    {
    
    public static void main(String[] args) 
    	{
    
    	String digitInput = JOptionPane.showInputDialog( "How many terms would you like to be used in the computation?");
    
    	Scanner termScan = new Scanner (digitInput);
    	
    	double terms = termScan.nextDouble();
    
    		
    		double pistart = 3;
    		double termCount = 0;
    		double num1 = 2;
    		double num2 = 3;
    		double num3 = 4;
    		double four = 4;
    
    		while (termCount <= terms)
    		{
    			
    		
    			double piloop = (four/(num1 * num2 * num3));
    			double finalpi = pistart + piloop;
    
    			num1 = num1 + 2;
    
    			num2 = num2 + 2;
    
    			num3 = num3 + 2;
    			
    			four = four * (-1);
    
    			termCount ++;
    			
    			JOptionPane.showMessageDialog(null, + finalpi);
    			
    		}	
    	}
    }
    		
    


Comments

  • Registered Users Posts: 117 ✭✭westmidlands


    Hi Ryan451,

    You almost have it perfect. The problem is you are always just taking piloop is and adding it to 3. piloop needs to be added to whatever value you got for pi from the previous iteration. Here is my code, the other suggestion I would make is now rename pistart to something better like piLatest:
    import javax.swing.JOptionPane;

    import java.util.Scanner;

    public class pi
    {

    public static void main(String[] args)
    {

    String digitInput = JOptionPane.showInputDialog( "How many terms would you like to be used in the computation?");

    Scanner termScan = new Scanner (digitInput);

    double terms = termScan.nextDouble();


    double pistart = 3;
    double termCount = 0;
    double num1 = 2;
    double num2 = 3;
    double num3 = 4;
    double four = 4;

    while (termCount <= terms)
    {
    double piloop = (four/(num1 * num2 * num3));
    pistart = pistart + piloop;

    num1 = num1 + 2;
    num2 = num2 + 2;
    num3 = num3 + 2;

    four = four * (-1);

    termCount ++;

    JOptionPane.showMessageDialog(null, + pistart);
    }
    }
    }



  • Registered Users Posts: 189 ✭✭Ryan451


    Hi Ryan451,

    You almost have it perfect. The problem is you are always just taking piloop is and adding it to 3. piloop needs to be added to whatever value you got for pi from the previous iteration. Here is my code, the other suggestion I would make is now rename pistart to something better like piLatest:

    Thanks man, really appreciate it.
    Just one final question, is it possible for only the final piLatest to be displayed, instead of an update every time around the loop?


  • Registered Users Posts: 141 ✭✭Mario852


    Ryan451 wrote: »
    is it possible for only the final piLatest to be displayed, instead of an update every time around the loop?

    You could declare the piLatest variable before the while loop and put your showMessageDialog after the while loop.

    So it looks like:

    double piLatest;
    while (termCount <= terms){ .... }
    JOptionPane.showMessageDialog(null, + piLatest);


  • Registered Users Posts: 189 ✭✭Ryan451


    Mario852 wrote: »
    You could declare the piLatest variable before the while loop and put your showMessageDialog after the while loop.

    So it looks like:

    double piLatest;
    while (termCount <= terms){ .... }
    JOptionPane.showMessageDialog(null, + piLatest);

    Ah, thanks so much man, really helped. Its crazy how tiny mistakes can throw off your code so much!


Advertisement