0

i have to write a program that displays the value of pi for values for i = 10000,20000.....,and 100000
i have to use the following series

pi = 4(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11_+...+(1/(2i +1))-(1/(2i +1))
any ideas

i have to do a program with looping i cant use arrays

can someone give me a hint and a strategy on how to do this problem

3
Contributors
40
Replies
43
Views
6 Years
Discussion Span
Last Post by NormR1
Featured Replies
  • 1

    That is the equation. Extract out the terms of the equation. I'll give you the first one: 4 Add the next 5 terms to the list, one per line Read More

  • 1

    What things change from one term to the next? Why do you keep writing that equation? I'm asking for a list of the terms, not the equation. Here are the first two terms: +4/1 -4/3 Read More

  • 1

    Your loop makes no sense. The equation you posted originally was to sum up 10000's of terms to get the value of pi. Take it one step at a time. What is the assignment statement to add a value to variable? That is what you need to do to sum … Read More

  • 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 … Read More

0

This your school exercise. Make an effort - describe the processes needed, write some pseudo-code (or real code if you prefer), and we will be happy to comment on it. However, YOU need to make a good faith first effort at solving the problem.

0

Look at the terms in the equation. Are there any repeating or stepped values that you can use in a loop?
Write a list of half a dozen terms with one term per line. Look for a pattern

0

hmmm ok

public class computingPi {
	public static void main(String args[]){
		double i, add, minus;
		i=0;
		add = (1/(2*i+1));
		for(int a=0;i<=10000 ; i++){
			double ans = 4*((1/(2*i+1)-(1/(2*i+1);
		}
	}

}
0

alright i have no idea what am i doing i no i have loop it but i dont think so thats correct

0

i see some stuff repeating
i know i have to do a for loop but im horrible and new to this

0

What is the pattern you saw when you listed the first half dozen terms in the equation?

You need to figure out the algorithm BEFORE you write the code.

0

4((1/(2i +1))-(1/(2i +1)+..........) is this the pattern i is always incrementing by 1

Edited by ilovejava: n/a

1

That is the equation. Extract out the terms of the equation. I'll give you the first one:
4

Add the next 5 terms to the list, one per line

Edited by NormR1: n/a

0

1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)+(1/17)-(1/19)
so after u plugged 4 which is 1/9 i did 5 6 7 8 9 i extracted the eqn in paper and plugged numbers, yes i see a pattern

Edited by ilovejava: n/a

0

I was trying to get you to put one term per line so you could see the pattern as you went from the value on one line to the next line.
Perhaps you can see it with the terms on one line.
To use a loop you need to see what changes from one term to the next.
Can you see that?

0

I was trying to get you to put one term per line so you could see the pattern as you went from the value on one line to the next line.
Perhaps you can see it with the terms on one line.
To use a loop you need to see what changes from one term to the next.
Can you see that?

yes i see the pattern (1/(2(1)-1) - (1/(2(2)-1) + (1/(2(3)-1) - (1/(2(3)-1) + (1/(2(4)-1)

is this the patern you wnat me to look at

0

Are you sure those are the first 5 terms? Where is the 4?

What things change from one term to the next?

0

Are you sure those are the first 5 terms? Where is the 4?

What things change from one term to the next?

4*(1/(2(1)-1) - 1/(2(2)-1) + 1/(2(3)-1) - 1/(2(3)-1) + 1/(2(4)-1))


sorry forgot to put the four in and arnt these the first 5 i plugged in 1 2 3 4 5

Edited by ilovejava: n/a

1

What things change from one term to the next?

Why do you keep writing that equation? I'm asking for a list of the terms, not the equation.
Here are the first two terms:
+4/1
-4/3

Edited by NormR1: n/a

0

What things change from one term to the next, starting with the second term?

Why do you keep writing that equation? I'm asking for a list of the terms, not the equation.
Here are the first two terms:
4
-4/3

it adds then subtracts
it adds(i=2) subtracts when(i=3) adds when(i=4) subtracts when (i=5)
i see for a odd number it subtracts for a even number it adds

but yeah its something like x-x+x-x+x-x

Edited by ilovejava: n/a

0

If the terms are:
+4/1
-4/3
+4/5
-4/7
I see that the sign alternates from one term to the next
and the value of the denominator increases by 2 for each term

What is a simple way to change the sign of a number?

0

If the terms are:
+4/1
-4/3
+4/5
-4/7
I see that the sign alternates from one term to the next
and the value of the denominator increases by 2 for each term

What is a simple way to change the sign of a number?

multiplying it by -1

0

Ok, you've got most of what you need. Now write the expression that adds the next term to the current sum and changes the sign for each term. Put it in a loop and see what you get.

0

Ok, you've got most of what you need. Now write the expression that adds the next term to the current sum and changes the sign for each term. Put it in a loop and see what you get.

do {
			ans = 1/(2i-1)*(-1)
			
			
			
		} 
		while (ans<=10000);
0

Your code does not sum the terms, it only computes a new one every loop.
How do you change the sign from term to term. This is the tricky bit:
Use a separate variable with the value of 1

What is this for:
while (ans<=10000);

Edited by NormR1: n/a

1

Your loop makes no sense.
The equation you posted originally was to sum up 10000's of terms to get the value of pi.

Take it one step at a time.
What is the assignment statement to add a value to variable? That is what you need to do to sum up the terms.

Edited by NormR1: n/a

0

ok norm this is what I got so far

public class computingPi {
	public static void main(String args[]){
		double var = 0, n,varM = 0, pi;
		n=1;
		pi = 0;
		for (double i =2; i<10000;i++){
			var -= (1.0/(2.0*i-1.0));
			var += (1.0/(2.0*i+1.0));

		}
		pi = 4.0*(1.0 + var);
		System.out.println("answer is " + pi);


	}

}
0

What does it print out?

You seem to have reworked the equation so I don't recognize it.
The equation was to sum these terms:
+4/1
-4/3
+4/5
-4/7
...
In the loop you change the denominator by 2 and toggle the sign.

I don't see where you do either of those in your code.

0

What does it print out?

You seem to have reworked the equation so I don't recognize it.
The equation was to sum these terms:
+4/1
-4/3
+4/5
-4/7
...
In the loop you change the denominator by 2 and toggle the sign.

I don't see where you do either of those in your code.

i get some number decimal = 2.3242432

0

i thought it is a summation thats what i need right and changes signs

0

I don't see that your code follows the equation. It does summations but I don't see where it changes the sign every time the denominator changes.
And it does NOT give you the correct answer.

I'd start over.

0

how bout the part where it says
var+=
var-=
isnt that sign change

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.