
March 3rd, 2013, 02:43 AM
#1
calculate pi
hi everyone I feel so stupid asking help for these.
i NEED HELP with these part of my code my prompt said :
(computing Π (PI) ) You can approximate (PI) value by using the following
series:
Π = 4( 1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + …  1/(2i 1) + 1/(2i + 1)
Write a method that calculate value of π by using the above series, using
the following header:
public static double pi()
assume i = 1000000
ok so I have been testing it but I really did get any far :
for ( double i = 1; i< 1000000; i+=2) {
// with these I try to increment the value of i to get to the serie.
//I did these after and test it and the output is all wrong and I know it
sum = (1/((2*i)+1));
sum = sum + (1/((2*i)1));
sum ++;
double pi = 4*(sum);
// I know is wrong I but I can´t figure it out what to do.
I am a first semester student of computer science I got the beging of the semester really fast but these is getting harder and my professor is not helping that much..
if someone can help me pleaseee!

March 3rd, 2013, 07:02 AM
#2
Re: calculate pi
Possibly its the maths that you need some help with. The general term in the series you are using is 1/(2 * n  1) where n starts at 1 and continues in increments of 1 until its terminating value  in your case 1000000. So when n = 1, the term is 1/(2 * 1  1) which is 1/1, n = 2 the term is 1/(2 * 2  1) which is 1/3 etc. Every even term is negative so when n is even the term is subtracted from the sum rather than added. The result is then four times the sum.
How a go with this info and post back your code if you still have problems. Please use [code] tags when posting code to make it readable (click go advanced, highlight the code and then click '#'). As it's homwork I can't really give you the code but I'll comment on anything you post back.

March 3rd, 2013, 02:43 PM
#3
Re: calculate pi
thankss
so I got these so far,
but I don´t know how to get the total value of the sum, and also if I should update the variable sum ?
Code:
public class Pi{
public static void main(String[] args)
{
sum =0;
for (int i=3 ; i< 1000000; i+=2){
if (sum%2==0){
sum = 1/(2 * i  1);
}
else {
sum = 1/(2 * i + 1);
}
}

March 3rd, 2013, 02:50 PM
#4
Re: calculate pi
That's not c++. That looks like java or c#? This is the c++ forum.

March 3rd, 2013, 03:00 PM
#5
Re: calculate pi
I know but I couldn´t find out a C# forum can u still give me feedback?

March 3rd, 2013, 03:19 PM
#6
Re: calculate pi
On the main forum page the c# forum is called csharp programming under the header .net programming. You need to scroll down the page until you find it. I really suggest you repost there for c# related questions.
However, the help I can give is this. You loop needs to start at 1 until 1000000 and go up in increments of 1. For every value of the loop you need to calculate the individual term using 1 / (2 * i  1). Then once you have calculated the term, add it to the sum if i is odd or subtract it from the sum if i is even.

March 3rd, 2013, 04:54 PM
#7
Re: calculate pi
thankss! I appreciate the help
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
