How can you make a program that finds pi?

And also, since pi is infinite, would your computer crash? lol

Im new with c++ xD
Lol first you can't "find" pi since it is infinite. Second you could make a program that estimates it to a high degree but it would take awhile and you would need to probably design your own variable to store it. The highest precision variable that is available as a default is double which has a precision of like 10 to the -38 or something. Hope that helps!
http://www.wikihow.com/Calculate-Pi-by-Throwing-Frozen-Hot-Dogs

No, seriously.




Also:
http://www.codeproject.com/KB/recipes/CRHpi.aspx
http://sourceforge.net/projects/projectpi/
http://www.ehow.com/how_5157582_calculate-pi.html

And, if you want to play with the Taylor Series in C++
http://www.cygnus-software.com/misc/pidigits.htm

Enjoy!

[edit]
BTW, you can't use processor types to do this (like double, etc). Use something like the GNU MP Bignum Library
http://gmplib.org/
Last edited on
It's definitely possible, after all, how would someone have computed pi to 100 million digits without a computer? I got into this problem a while ago, and it turns out there are a ton of ways to find pi. For a few ways check out http://en.wikipedia.org/wiki/Pi#Calculating_.CF.80 The funnest method by far that I've found, however, is to utilize a random number generator and throw virtual "darts" at a circular dart board encompassed by a square. By generating a point within the square and testing its distance from the center of the square and circle, you can find the proportion of darts that land within the square, but outside the circle. The proportion will be a proportion of their areas and you can then use some simple algebra to solve for pi. The longer the program runs, the more accurate its estimate. This method takes about a day to calculate 4 or 5 digits of pi in TI-84 BASIC before the batteries runs out. Obviously not the most efficient method, but sometimes that isn't the point. Happy hunting :)
O_O
Uhh... Wouldn't it be faster to do a left-right, top-bottom sweep of the square, rather than waste time randomizing samples?
For example:
1
2
3
4
5
6
7
8
9
10
int inside=0,
	outside=0;
for (int y=0;y<side;y++){
	for (int x=0;x<side;x++){
		if (isInside(side,x,y))
			inside++;
		else
			outside++;
	}
}

With greater 'side', greater accuracy.

EDIT: Oh, and pi isn't infinite, it has infinite many fractional digits. It's a transcendental number.
Last edited on
Nice code, the advantage of mine is that it's more fun, and less efficient. Watching it grow ever so slightly closer to the actual value and randomly being thrown way off every time you think the program is getting somewhere can be quite addicting ;) It adds a bit of drama to the mix, which I think is a bit more entertaining. I'll have to remember that solution though.
Topic archived. No new replies allowed.