#include #include #include int GLS(); int GLA(); int BBP(); int RAM(); int factorial(int number); int main(int argc, char *argv[]) { int method; int i; i = 0; while(i==0) { printf("Select a method to calculate pi.\n 1 - Gregory-Leibniz Series\n 2 - Gauss-Legendre Algorithm\n 3 - Bailey-Borwein-Plouffe Formula\n 4 - Ramanujan Series\n"); printf("Please type in a number: 1,2,3,4\n"); scanf("%d", &method); switch(method) { case 1: GLS(); break; case 2: GLA(); break; case 3: BBP(); break; case 4: RAM(); break; default: printf("Selection not valid.\n"); break; } printf("\n\nWould you like to try again? Y/N ? \n"); scanf("%s", &method); switch(method) { case 'Y': printf("\n\n"); break; case 'y': printf("\n\n"); break; case 'N': { i=1; break; } case 'n': { i=1; break; } default: printf("\nSelection not valid.\n\n"); break; } } system("pause"); return 0; } int GLS() { double pi; pi = 0; int n; int terms; int digits; printf("Select the number of terms in the series.\n"); scanf("%d", &terms); printf("Select the number of digits to display. \n"); scanf("%d", &digits); for(n = 0; n < terms; n++) { pi = pi + 4*pow(-1,n)/(2*n+1); } printf("\n%1.*f\t\n", digits, pi); return 0; } int GLA() { double a1,a2; double b1,b2; double t1,t2; double p1,p2; double pi; int n; int terms; int digits; printf("Select the number of iterations of the algorithm.\n"); scanf("%d", &terms); printf("Select the number of digits to display. \n"); scanf("%d", &digits); a1 = 1; b1 = 1/sqrt(2); t1 = .25; p1 = 1; for(n = 1; n < terms + 1; n++) { a2 = .5*(a1+b1); b2 = sqrt(a1*b1); t2 = t1 - p1*pow(a1-a2,2); p2 = 2*p1; a1 = a2; b1 = b2; t1 = t2; p1 = p2; } pi = pow(a1+b1,2)/(4*t1); printf("\n%1.*f\t\n", digits, pi); return 0; } int BBP() { int k; double pi; int terms; int digits; pi = 0; printf("Select the number of terms in the series.\n"); scanf("%d", &terms); printf("Select the number of digits to display. \n"); scanf("%d", &digits); for(k=0;k < terms ;k++) { pi = pi + pow(.0625,k)*(120*pow(k,2)+151*k+47)/(512*pow(k,4)+1024*pow(k,3)+712*pow(k,2)+194*k+15); } printf("\n%1.*f\t\n", digits, pi); return 0; } int RAM() { double pi; int k; double invpi; int terms; int digits; printf("Select the number of terms in the series.\n"); scanf("%d", &terms); printf("Select the number of digits to display. \n"); scanf("%d", &digits); for(k=0;k