/* * Archimedes' calculation for the perimeter of the polygons circumscribing * a circle, polygons with 6, 12, 24, 48, 96 ... sides (Archimedes goes up * to 96). Assuming a circle of radius 1, this approximates pi from above. */ #include "stdio.h" #include "conio.h" #include "math.h" #define M_PI 3.14159265358979323846 #define K 14 // LIM = 6 * 2^K = 6, 12, 24, 48, ... #define LIM 6 * (1 << K) void main(int argc, char* argv[]) { int n = 0; double p = sqrt(3.0) / 3; double q = 2 * p; printf("%5s %7s %16s\n", "n", "pi_n", "error (over)"); printf("-------------------------------\n"); for (n = 6; n <= LIM; n *= 2) { double perim_out = n * p; double error = perim_out - M_PI; printf("%5d %1.9f %1.9f\n", n, perim_out, error); // Next p and q (for 2n). p = p / (q + 1); q = sqrt(p * p + 1); } _getch(); }