Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Computing Pi!

  • 28-10-2013 05:29PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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