Skip to content

Instantly share code, notes, and snippets.

@martinlk
Created February 14, 2012 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinlk/1829558 to your computer and use it in GitHub Desktop.
Save martinlk/1829558 to your computer and use it in GitHub Desktop.
PI calculation to 29 digits using recursive fractional method
#include <stdio.h>
#include <math.h>
#define ld __float128
ld pie(int level);
ld cake(int level);
void sho(ld a);
//
// I borrowed sho() from:
// http://mathforum.org/kb/thread.jspa?threadID=2229919&messageID=7363928
//
int main()
{
ld pi = 16 / cake(0) - 4 / pie(0);
sho(pi);
return 0;
}
ld pie(int level)
{
if(level > 15) return 1;
ld x = (478*level)+239 + (level+1)*(level+1) / pie(++level);
return x;
}
ld cake(int level)
{
if(level > 15) return 1;
ld x = (10*level)+5 + (level+1)*(level+1) / cake(++level);
return x;
}
void sho (ld a) {
long double c = floor ((long double)(1e10Q * a));
ld d = c, f = a - d/1e10Q, g=1e30Q * f;
printf("%0.10Lf%020.0Lf\n", c/1e10L, (long double)g);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment