Search code examples
javarecursionwhile-looppi

Calculating Pi using a while loop and recursive method (Java)


My assignment was to calculate Pi using an algorithm he gave us in class, determine correct digits, and estimate Pi to six digits using a while loop and a recursive method. But my "super smart professor" didn't tell us a bloody thing about recursive methods and when I email him, he gets angry at me for not getting it just by looking at it. This is my code so far, I left out my while loop method and recursive method because I have no idea how to do those.

public static final double REAL_PI = 3.14159;//PI is the value prof gave us on the handout
public static double Pi = 0; //Pi is the value of Pi that this program calculates
public static int m = 0; 

public static void main (String [] args)
{
    Algorithm(); //calls on method of calculating pi
    System.out.println("Calculated pi: " + Pi); //prints out pi
    countDigits(Pi); //calls on countdigits method
    System.out.println("Number of digits: " + c); //has the computer print out the count because that's how many digits are the same
    PiRecur(); //calls on estimate digits method
}

public static double Algorithm() //should return a double (pi)
{
    for(m=1; m<=100000; m++)
    {
        Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));//Math.pow uses math package to calculate a power to use the algorithm
    }
    return Pi;
}

public static int countDigits (double Pi)
{
    int a = (int) Pi; //the int cast makes Pi and REAL_PI into integers so the program can compare each digit separately 
    int b = (int) REAL_PI;
    int c = 0;
    int count = 0;
    while(a == b)//if m less then or equal to 100,000 then while loop runs
    {
        count ++;
        a = (int) (Pi*(Math.pow(10,count))); //if a=b then the computer will multiply Pi and REAL_PI by 10 
        b = (int) (REAL_PI*(Math.pow(10,count)));
        /*when you input a and b 
         * while loop compares them
         * if a = b then loop continues until a doesn't equal b and loop ends
         */
    }
    c = count; //gives c the value of the count so it can be used outside the method
    return count;
}

}


Solution

  • I'm not sure how a solution that uses a while loop and recursion would loop like, since I can't read your professor's mind, but I can think of two different solutions that use one or the other.

    Using while loop :

    You don't run your algorithm an arbitrary number of iterations (100000 in your example) and hope that you got close enough to the expected result. You use a while loop, and on each iteration you check if your current calculation of Pi is close enough to your target.

    public static double Algorithm()
    {
        int m = 1;
        double Pi = 0.0;
        while (countDigits(Pi) < 6) {
            Pi += 4*(Math.pow(-1, m-1)/((2*m)-1)); // I'm assuming this formula works
            m++;
        }
        return Pi;
    }
    

    Using recursion :

    The same solution can be translated into a recursion. This time, you supply an initial index m (1) and an initial value of Pi (0) to Algorithm. The method adds the m'th term to Pi. If the new value of Pi is not good enough (determined by countDigits), you make a recursive call that would add the m+1th term to Pi and check again if the new value is good enough. The recursion would stop when the value of Pi is 6 digits accurate.

    public static double Algorithm(int m,double Pi)
    {
        Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));
        if (countDigits(Pi) < 6)
            Pi += Algorithm(m+1,Pi);
    
        return Pi;
    }
    

    You call the method with :

    Algorithm (1,0.0);