0

Yes that code uses different signs.
But the whole of the code does not follow the equation and produce the correct answer.
Add a println after each of those assignments to print out the value of var.
Shorten the loop to 10 times and look at the output

0

Yes that code uses different signs.
But the whole of the code does not follow the equation and produce the correct answer.
Add a println after each of those assignments to print out the value of var.
Shorten the loop to 10 times and look at the output

wow when i shortened the loop the numebr got bigger

0

Throw out that program and start over. You have 90% of what you need for the program.

Look again at the terms that are being added each loop.
There are 2 things that change every iteration.

0
public class computingPi {
	public static void main(String args[]){
		double var = 0,  pi;
		
		pi = 0;

		for(int count = 10000; count<=100000;count= count + 10000){
			for (int i =1; i<=count;i+=2){
				var +=(1.0/(2.0*i-1.0)-1.0/(2.0*i+1.0));
		
			
			
		}
		pi = 4* var;

		System.out.println("answer is " + pi);
		
		}

	}

}

this still doesnt work not the outer for loop changes the the count

1

I don't know why you are trying to use two loops. One will do it.

Try having the code in your loop add a single term to the sum each time.
The terms to be added are as I've posted many times:
+4/1
-4/3
+4/5
-4/7
...

If you generalize the above terms you'd get:
sign * 4 / denominator

Besides summing these terms one per loop, you need to add 2 to the denominator and change the sign.

0

I don't know why you are trying to use two loops. One will do it.

Try having the code in your loop add a single term to the sum each time.
The terms to be added are as I've posted many times:
+4/1
-4/3
+4/5
-4/7
...

If you generalize the above terms you'd get:
sign * 4 / denominator

Besides summing these terms one per loop, you need to add 2 to the denominator and change the sign.

only reason im having two loops is cuz teacher wants value of pi when count =10000,20000,30000,40000.....100000
alright let me try that

0

You could have an if test inside of the loop to print the current value of pi when the loop counter is at those numbers.

0

yes i got it thank you

public class computingPi {
	public static void main(String args[]){
		double var = 0;
		
		for (int i =0; i<=100000;i++){
			var += (Math.pow((-3), (-i)))/(2*i + 1) ;
		
			if (i % 10000 == 0) {
				System.out.println("after the  count is "+i+" PI is   " + Math.sqrt(12)*var);
			
			}
		}

	}

}
0

Where did you get that equation from?
It doesn't look anything like the one you posted earlier.

It surely doesn't follow this series:
pi = 4(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11_+...+(1/(2i +1))-(1/(2i +1))

Edited by NormR1: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.