There's some proof out there that states that the probability that two random selected integers being relative prime is 6/π2
You can use this to write a program that estimates π for you:
#include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #define LIST_SIZE 500000000 using namespace std; bool rel_prime(int a, int b); int main() { srand(time(NULL)); int x, y; int acc; double pi; for (int i = 0; i < LIST_SIZE; i++) { x = rand() % LIST_SIZE + 1; y = rand() % LIST_SIZE + 1; if (rel_prime(x, y)) acc++; } pi = sqrt(((double)1) / ((((double)(acc))/((double)(LIST_SIZE))) * 0.1666666)); //could be written better cout << pi << endl; return 0; } bool rel_prime(int a, int b) { while(true) { if (!(a %= b)) return b == 1 ; if (!(b %= a)) return a == 1 ; } }
It selects a random pair of integers 500,000,000 times to get an accurate estimate. Mine came up with 3.14156...which is pretty darn close =P
I found out you can also derive π from other formulas. For example, picture a circle inscribed inside a square. By choosing a random point inside the square the probability that it will be inside the circle is πr2/4r2 = π/4...but that's a more resource intensive method.
So...disucuss cool math stuff =P