SHARE
TWEET

Untitled

a guest Jan 20th, 2020 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* usage only for people who do not care about licenses */
  2. /* also it's an attempt on an algorithm that should probably
  3. be considered public domain */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <stdbool.h>
  9.  
  10. double find_pi (int i, bool f) {
  11.  int    n=0;
  12.  double a=1, b=1/sqrt(2), t=0.25, p=1, an, r;
  13.  for (n=0; n<i; n++) {
  14.   an = (a+b) / 2;
  15.   b  = sqrt(a*b);
  16.   t -= p * pow((a-an),2);
  17.   p *= 2;
  18.   a = an;
  19.   r = pow ( a+b , 2 ) / (4*t) ;
  20.   n ++ ;
  21.   if (f) { printf ( "%d %f %f \n", n, fabs(a-b), r ) ; }
  22.   }
  23.  return r;
  24.  }
  25.  
  26. int main(void) {
  27.  printf("%f\n", find_pi(3,true));
  28.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top