/* * Archimedes' calculation for the perimeter of the polygons inscribed in * 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 below. */ #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 r = 1.0; double s = sqrt(3.0); double k = 1.0; printf("%5s %8s %15s\n", "n", "pi_n'", "error (under)"); printf("--------------------------------\n"); for (n = 6; n <= LIM; n *= 2) { double perim_in = n * r / 2; double error = M_PI - perim_in; printf("%5d %1.9f %1.9f\n", n, perim_in, error); // Next r and s (for 2n). k = (2 + s) / r; r = 2 / sqrt(k * k + 1); s = k * r; } _getch(); }