0

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;
}

}

3
  • 2
    So what is your question? Commented Oct 9, 2014 at 18:15
  • how do you estimate pi to 6 digits using a while loop and a recursive method using a different algorithm? Commented Oct 9, 2014 at 18:24
  • 1
    Please stick to Java naming conventions. This code incomprehensible - methods should be in camelCase. Pascal case is reserved for classes. Commented Oct 9, 2014 at 18:32

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

the recursive method keeps giving me a stack overflow error for the if statement
@KazuoKatiushi Stack overflow means the recursion doesn't stop. I can't see any obvious bug just looking at the code. You should try to add some debug prints or run the method with a debugger, and see how the value of Pi changes with each recursive call.
The formula is correct. Just be sure to start from 1; starting from 0 will yield pi + 4 instead of pi.
yea I just went through and found it starting from 0 and threw off my code a bit but thanks for helping me out!!

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.