# pie calcualation project, almost done, just need some help

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 02-14-2008
DougDbug
pjr5043,

This is not a simple formula that you can plug into your program, but rather an infinite series. (To be honest, I don't understand the notation your teacher gave you.) You can't make a calculation with an infinite number of elements, but with a computer, you can easily carry this series out to 100 or 100,000 elements.

Here is a reference.

The series is equal to one-quarter of PI, and it looks like this:
1 - 1/3 + 1/5 - 1/7 + 1/9 -1/13 +1/17...

Can you see what's next? ...-1/19 +1/21 -1/23...

You need to write a loop does this automatically for 100, 1,000 or 100,000 elements. Each time you go through the loop, you will be adding-and-subtracting smaller-and-smaller fractions. In other words, your program will be figuring-out the fractions... For example, you won't see "1\13" or any of those fractions anywhere in your C++ code... You might see "1/i". Does that make sense?

I assume you know how to write a loop that increments a number, or adds 5 to a number every time through the loop... If you don't know how to do that, you need to re-study loops before you jump into this project.
• 02-14-2008
brewbuck
Quote:

Originally Posted by iMalc
What on earth made you think you should calculate pi by starting with a number very close to pi and constantly multiplying that by some other number? Surely you can see that all this does is increase that number quadratically? Even if it could work it would be cheating since you almost start with the answer anyway.

I think the equation was written wrong. It should be:

pi = 4 * (1 - 1/3 + 1/5 - 1/7...)

(Which is true, by the way)

And it looks like an assignment, not something that was thought up.
• 02-15-2008
Make Pi a define(ed) value in your program, and then don't touch it until right at the end when you compare your value for Pi, to the real (close approximation) of Pi. You know your equation is goofed, I believe. You should be having - then a +, then another - and +, throughout the series, iirc.

All the rest of your program except one line of the series equation, is rubbish - junk it.

Use the while loop to work with the digits after the decimal place, and only do the multiplication, (* 4), right at the end.
• 02-15-2008
manav
also this is very inaccurate method
very naive method of finding pi :(
see my output . . .
Code:

```>>> mpi(10) pi 3.04183961893 >>> mpi(100) pi 3.13159290356 >>> mpi(1000) pi 3.14059265384 >>> mpi(10000) pi 3.14149265359 >>> mpi(100000) pi 3.14158265359 >>> mpi(1000000) pi 3.14159165359 >>> mpi(10000000) pi 3.14159255359 >>>```
• 02-15-2008
It's slow as a dog, but if you carry the arithmetic out far enough, it does find the correct digits.

It's just S-L-O-W as a snail if you want a lot of digits, compared to many other algorithms to find Pi.
• 02-15-2008
DougDbug
pjr5043,

Start with a program that simply "calculates" 1/3 by using an infinite series in a loop. As you know, 1/3 is represented in decimal by the infinite series 0.3333... So, the series looks like this:
3/10 + 3/100 + 3/1,000 + 3/10,000... (Or, like this: 0.3 + 0.03 + 0.003 + 0.0003... But, you should use fractions... I should say division... in your program.)

The first time though the loop, you will get 0.3*. The 5th time through the loop you will get 0.33333*, etc.

Once you get that working, you can modify the code inside you loop to calculate the Taylor series.

* You won't get these exact numbers, due to the way floats and doubles are stored in binary. So, don't worry if you see something like 0.33299999999999 instead of 0.333.
• 02-19-2008
pjr5043
4*(1- 1/2i-1 + 1/2i+1) .. thats the equation, i just need to figure out how to implement it.. this stuff is pretty hard
• 02-19-2008
tabstop
Quote:

Originally Posted by pjr5043
4*(1- 1/2i-1 + 1/2i+1) .. thats the equation, i just need to figure out how to implement it.. this stuff is pretty hard

That is by no means the equation. The equation is: pi = 4*(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + (-1)^i/(2*i+1) + ...). In summation notation, the right hand side is (bad ASCII art alert):
Code:

```  inf   -----   \      (-1)^i 4* >    ------   /      2i+1   -----   i=0```
Summations are trivially easy to code up using for-loops (start with the sum equal to 0; each time through the loop add up the bit off to the right). Multiplication by 4 is not that hard either. Of course, we won't be able to stop the loop at infinity; we'll have to settle for the first hundred (or thousand, etc.) terms as given in your assignment.
• 02-19-2008
pjr5043
alrite guys, i'm super confused with all of this right now.

This is what my teacher told me today:
She said that this "4*(1- 1/2i-1 + 1/2i+1)" was the equation i needed to use. She then said that this equation needed to go to a loop and add by increments of 2 each time, so i figured make a variable like c, then do c+2. I really can't get it to work.

To be honest, if someone on here can just give me an equation to use, i'll paypal you \$5, that's how bad i need this right now, i have so much other work to do, and i'm just never going to understand this project.

This is the exact description i was given:

Description : You can approximate π by using the following series :
π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ........ - 1/(2i - 1) + 1/(2i + 1) )

Write a program that displays the π value for i = 100, 1000 and 100000

note : find the pattern in the series.
• 02-19-2008
tabstop
We've put the equation up here six times, and you just posted it yourself. Note how this:
Quote:

4*(1- 1/2i-1 + 1/2i+1)
and this
Quote:

π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ........ - 1/(2i - 1) + 1/(2i + 1) )
are not in fact the same thing, which is what we've been trying to tell you all along.

One last time:
when i is 1: 1/(2i-1) - 1/(2i+1) = 1 - 1/3
when i is 3: 1/(2i-1) - 1/(2i+1) = 1/5 - 1/7
when i is 5: 1/(2i-1) - 1/(2i+1) = 1/9 - 1/11

So to get 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + 1/(2i-1) - 1/(2i+1) you should do ..............
• 02-19-2008
pjr5043
i would need to have i equal all numbers from 1 to 100? than 1000 and 100000?
• 02-19-2008
tabstop
Quote:

Originally Posted by pjr5043
i would need to have i equal all numbers from 1 to 100? than 1000 and 100000?

If you're going to do two at a time (with a + and a -), then you have to go by twos. You can let i be every number, but then you'll have to take care of the alternating + and - yourself (with sign = -1*sign or whatever).
• 02-19-2008
pjr5043
alrite, so i can put like int i+2 in the loop right? .. but then what should my command line be for the loop .. right now i have c!=100 is that right?
• 02-19-2008
tabstop
Quote:

Originally Posted by pjr5043
alrite, so i can put like int i+2 in the loop right? .. but then what should my command line be for the loop .. right now i have c!=100 is that right?

No and no. If you're doing a for loop you would need to