Compute pi
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
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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.
rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
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
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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);
}
}
}
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
alright i have no idea what am i doing i no i have loop it but i dont think so thats correct
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
i see some stuff repeating
i know i have to do a for loop but im horrible and new to this
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
4((1/(2i +1))-(1/(2i +1)+..........) is this the pattern i is always incrementing by 1
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
Are you sure those are the first 5 terms? Where is the 4?
What things change from one term to the next?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
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.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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);
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2