Search code examples
javapi

Pi with Nilakantha method


I am trying to calculate pi with the Nilakantha method. Whenever I run this program I get -Infinity if I input 1 and anything else I get NaN. I am trying to modify my program that uses the Leibniz method, and I'm very new to java.

I appreciate all help!

    public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number of calculations you would like to do");


    long no = Long.parseLong(reader.readLine());
    long cycle = 0;
    long w = 2;
    long x = 3;
    long y = 4;
    long z = 4;
    long odd=1;
    long i=1;
    long a = 1;
    long b = 1;
    double pi= 0.0;

    for(;i<=no;i++)
    {
        a = w*x*y;
        b = x*y*z;
        double currentTerm=0.0;
        if (i%2==0)
        {    
            currentTerm=(double)4/a;
            cycle = cycle+1;
            w = w+1;
            x = x+1;
            y = y+1;       
        }    
        else
        {
            currentTerm=(double)-4/b;
            cycle = cycle+1;
            x = x+1;
            y = y+1;
            z = z+1;        
        }    
        odd=odd+2;
        pi = pi+currentTerm;

    }

    System.out.println("You calculated that pi is");
    System.out.println(pi);
    System.out.println(3.1415926535897932);
    System.out.println("Pi is actually");

    double error = pi/3.1415926535897932;
    if(error >= 1) {
    double bigerror=2-error;
    System.out.println("Your accuracy is");
    System.out.println(bigerror*100);
    System.out.println("percent");
    System.out.println(cycle);
    }
    else { 
    System.out.println("Your accuracy is");
    System.out.println(error*100);
    System.out.println("percent");
    System.out.println(cycle);
                }
             }
        }  

Solution

  • On your first iteration a and b are both zeros.

    I don't think you have your initialization part correct.

    http://helloacm.com/two-simple-equations-to-compute-pi/

    Here I see that j starts from 2. You have zeroes.

    Make sure you implement the algorithm correctly.

    Here is your code corrected.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class MainProgram {
    
        public static void main(String[] args) throws Exception {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the number of calculations you would like to do");
    
            long no = Long.parseLong(reader.readLine());
            long step = 0;
            double ans = 3;
            long j = 2;
    
            double pi = 0.0;
    
            while (true) {
                step++;
                if ((step % 2) == 1) {
                    ans += 4.0 / (1.0 * j * (j + 1) * (j + 2));
                } else {
                    ans -= 4.0 / (1.0 * j * (j + 1) * (j + 2));
                }
    
                j += 2;
                pi = ans;
    
                if (step >= no)
                    break;
            }
    
            System.out.println("You calculated that pi is");
            System.out.println(pi);
            System.out.println("Pi is actually");
            System.out.println(3.1415926535897932);
    
            double error = pi / 3.1415926535897932;
            if (error >= 1) {
                double bigerror = 2 - error;
                System.out.print("Your accuracy is: ");
                System.out.print(bigerror * 100);
                System.out.println(" percent");
                System.out.println(step);
            } else {
                System.out.print("Your accuracy is: ");
                System.out.print(error * 100);
                System.out.println(" percent.");
                System.out.println(step);
            }
        }
    }