Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* usage only for people who do not care about licenses */
- /* also it's an attempt on an algorithm that should probably
- be considered public domain */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdbool.h>
- double find_pi (int i, bool f) {
- int n=0;
- double a=1, b=1/sqrt(2), t=0.25, p=1, an, r;
- for (n=0; n<i; n++) {
- an = (a+b) / 2;
- b = sqrt(a*b);
- t -= p * pow((a-an),2);
- p *= 2;
- a = an;
- r = pow ( a+b , 2 ) / (4*t) ;
- n ++ ;
- if (f) { printf ( "%d %f %f \n", n, fabs(a-b), r ) ; }
- }
- return r;
- }
- int main(void) {
- printf("%f\n", find_pi(3,true));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement