/* Approximating Pi Pi ~= 2^n * sin(pi/2^n); //this uses pi in definition, so we have to calculate sin(pi/2^n) sin(pi/2^n) 2^n sqrt(2)/2 4 sqrt(2-sqrt(2))/2 8 sqrt(2-sqrt(2+sqrt(2)))/2 16 sqrt(2-sqrt(2+sqrt(2+sqrt(2))))/2 32 --recursive part is how many times +sqrt(2) is added */ #include #include #include #define N 20 //limited because of the double data type and that we use 2^N double getapprox(int n) { double ret; if(n==4) return 0; return sqrt(2+getapprox(n/2)); } double approxPI(int n) { double val = pow(2.0, n); double res = getapprox(val); return val*0.5*sqrt(2.0-res); } int main() { double x = approxPI(N); printf("pi at %d approximation is %.10lf\n", N, x); system("pause"); }