Skip to content

Instantly share code, notes, and snippets.

@lukakostic
Created August 22, 2018 22:38
Show Gist options
  • Select an option

  • Save lukakostic/7f04af9e6fba2643e52bb8236716341b to your computer and use it in GitHub Desktop.

Select an option

Save lukakostic/7f04af9e6fba2643e52bb8236716341b to your computer and use it in GitHub Desktop.
Print digits of pi using the Leibniz formula: pi = 4 * (1 -1/3 +1/5 -1/7 ....)
//Print digits of pi using the Leibniz formula: pi = 4 * (1 -1/3 +1/5 -1/7 ....)
#include <stdio.h>
#define NumType long double
int main(){
NumType d = 1.0;
NumType f = 0.0;
NumType pi = 0.0;
NumType dotRemover = 30000000000000000.0;
for (long long i = 0; (i + 10000000000000000) > 0; i++)
{
f = (NumType)1.0 / (((NumType)3.0) + ((NumType)i) * ((NumType)2.0));
if (i % 2 == 0)
d -= f;
else
d += f;
if (i % 100000000 == 100)
{
pi = ((NumType)40000000000000000.0) * d;
pi = pi - dotRemover;
printf("3.%Lf\n", pi);
}
}
while (2>0) {}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment