Computing Pi Java Program

I take my first Java programming class and this is my first class. I am so confused about how to approach him. Any help or correction would be appreciated.

You can approximate the value of the PI constant using the following series:

PI = 4 ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + ( (-1)^(i+1) )/ (2i - 1) )

Ask the user for the value of i (in other words, how many terms in this series to use) to calculate PI. For example, if the user enters 10000, sum the first 10000 elements of the series and then show the result.

In addition to displaying the final result (your final PI approximation), I want you to display as your intermediate calculates at each power 10. Thus, 10, 100, 1000, 10000, etc. are mapped onto the PI approximation shielding with so many elements to be summed.

This is what I have done so far.

import java.util.Scanner;
        public class CalculatePI {

            public static void main(String[] args) {
                // Create a Scanner object
                Scanner input = new Scanner (System.in);

                // Prompt the user to enter input
                System.out.println("Enter number of terms");
                double i = input.nextDouble(); // value of i user entered
                    double sum = 0;
                for(i=0; i<10000; i++){
                           if(i%2 == 0) // if the remainder of `i/2` is 0
                           sum += -1 / ( 2 * i - 1);
                        else
                           sum += 1 / (2 * i - 1);
    }

                        System.out.println(sum);


    }
}
+4
source share
5 answers

The first thing I see is an attempt to return a value from your method void main.

not return pi;from your main method. Print it out.

System.out.println(pi);

Secondly, when people write a cycle for, they usually repeat through i, which probably matches the one iyour professor talked about in the formula

for(double i=0; i<SomeNumber; i++)
{
    sum += ((-1)^(i+1)) / (2 * i - 1) );
}

now it will not work correctly as it is, you still have to process ^that java does not use initially. fortunately for you, -1^(i+1)is a variable number, so you can just use the operatorif

for(double i=0; i<SomeNumber; i++)
{
    if(i%2 == 0) // if the remainder of `i/2` is 0
        sum += -1 / ( 2 * i - 1);
    else
        sum += 1 / (2 * i - 1);
}
+8
source

- . , 0, , .

for(int counter=1;counter<input;counter++){
    sum += Math.pow(-1,counter + 1)/((2*counter) - 1);
}

, , (, 1000, 10000, 100000).

+2

. , . , , :

if(i%2 == 0)
    sum += -1 / ( 2 * i - 1); 
else
    sum += 1 / (2 * i - 1);

- ,

-1 ^ (i + 1).

, . , . , , . , 1-, 4 .

: , , Math.pow(, ) . , 2 ^ 3 - Math.pow(2, 3). :

PI = 4 * (1 - 1/3 + 1/5 + ... + Math.pow(-1, i + 1) / (2 * i - 1));

PI: for PI for :

for (int i = 10000; i > 0; i--) {
      PI += Math.pow(-1, i + 1) / (2 * i - 1); // Calculate series in parenthesis
      if (i == 1) { // When at last number in series, multiply by 4
        PI *= 4;    
        System.out.println(PI); // Print result
      }  
}

, 10000 , = 1. , Java - , 1 . , , . 4 .

, 10000 :

java.util.Scanner input = new java.util.Scanner(System.in); //You can import instead
System.out.println("Enter number of loops: ");
int loops = input.nextInt();

for (int i = loops; i > 0; i--) {
...
+2

// , , .

int count = 10;
int m = 1;
int n = 0;
double pi = 0.0;
final double SQRT_12 = Math.sqrt(12);
while (count > n)
{
    pi += SQRT_12 * (Math.pow(-1, n)/(m * Math.pow(3, n)));
    System.out.println(pi);
    m += 2;
    n++;
}
0
    double Pi = 0.0;
    long start = System.currentTimeMillis();
    for (double i = 1; i < 100000000; i++) {
        Pi += (i % 2 == 0) ? -1 / (2 * i - 1) : 1 / (2 * i - 1);
    }
    long stop = System.currentTimeMillis();
    System.out.println(Pi * 4);
    System.out.println(Math.PI);
    System.out.println(stop-start);
    // output:
    // 3.141592663589326
    // 3.141592653589793
    // 2426
0

All Articles