playground/c/pi-val-ramanjuan.c

56 lines
1.1 KiB
C

// Originally written in 2015
#include<stdio.h>
#include<math.h>
double pow1(int base, int power)
{
double p=1;
int j=0;
for(; j<power ; ++j)
{
p*=base;
}
return p;
}
double factorial(int a)
{
int j=a;
double f=1;
for(; j>1; --j)
{
f*=j;
}
return f;
}
int main()
{
int k, i;
double pi=0;
printf("Enter the value of k: ");
scanf("%d", &k);
for(i=0; i<k; ++i)
{
pi+=((factorial(4*i) * (1103 + (26390*i))) / (pow1(factorial(i), 4) * pow1(396, 4*i)) );
}
pi*=(double) ( ( 2 * sqrt(2) ) / ( 9801 ) );
pi = (double) ( 1 / pi );
printf("The value of pi using Ramanujan's equation is: %.20lf\n", pi);
return 0;
}
/*
Enter the value of k: 1
The value of pi using Ramanujan's equation is: 3.14159273001330552333
Enter the value of k: 2
The value of pi using Ramanujan's equation is: 3.14159265358979356009
Enter the value of k: 10
The value of pi using Ramanujan's equation is: 3.14159265358979311600
Enter the value of k: 100
The value of pi using Ramanujan's equation is: -nan
*/