Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GMP.h>
int main ()
{
mpf_set_default_prec (15);
int x=1; //loop
mpf_t k; //series variable (should start equal to 1)
mpf_init(k); //set equal to 0 *I THINK*
mpf_t first_term, second_term, third_term, fourth_term, fifth_term; //terms of series broken down to guarenteed proper order of opperations (revised from v0.1)
mpf_t pi_term;
mpf_t final_pi;
while(x <= 10)
{
//beginning of series
//first_term = (1)/pow(16,k);
//mpz_mul_ui(first_term,16,k);
//mpf_ui_div(first_term,1,first_term);
//second_term = (4)/((8*k)+1);
mpf_mul_ui(second_term,k,8);
mpf_add_ui(second_term,second_term,1);
mpf_ui_div(second_term,4,second_term);
//third_term = (2)/((8*k)+4);
mpf_mul_ui(third_term,k,8);
mpf_add_ui(third_term,third_term,4);
mpf_ui_div(third_term,2,third_term);
//fourth_term = (1)/((8*k)+5);
mpf_mul_ui(fourth_term,k,8);
mpf_add_ui(fourth_term,fourth_term,5);
mpf_ui_div(fourth_term,1,fourth_term);
//fifth_term = (1)/((8*k)+6);
mpf_mul_ui(fifth_term,k,8);
mpf_add_ui(fifth_term,fifth_term,6);
mpf_ui_div(fifth_term,1,fifth_term);
//pi_term = second_term - third_term - fourth_term - fifth_term;
mpf_sub(pi_term,second_term,third_term);
mpf_sub(pi_term,pi_term,fourth_term);
mpf_sub(pi_term,pi_term,fifth_term);
//pi_term = pi_term * first_term;
mpf_mul(pi_term,pi_term,first_term);
//end of series formula, at this point, pi_term is result of plugging value in for k
//final_pi = final_pi + pi_term; //Adds results from series
mpf_add(final_pi,final_pi,pi_term);
mpf_clear (first_term);
mpf_clear (second_term);
mpf_clear (third_term);
mpf_clear (fourth_term);
mpf_clear (fifth_term);
mpf_clear (pi_term);
mpf_add_ui(k,k,1); //k increases incrementally to 100,000 (should be more, but this program only does to 15 decimal places, so it is not necesary)
x++; //x increases incrementally to satisfy while() loop, will break from loop at x<=100000
}
system("CLS"); //clears command prompt/terminal window, change to (system("CLEAR"); in Mac)
printf("Pi Calculator v0.2 By Bryan Keller\nSource code available upon request\nFormula used: Bailey Borwein Plouffe formula (BBP formula)\n");
gmp_printf("\n\n%Fd\n\n",final_pi);
system("PAUSE");
return 0;
}
As of right now, I have first_term related code turned off, simply because I'm not sure how I will go about implementing 16 to the K power using GMP.