Created
October 27, 2017 13:36
-
-
Save onyxblade/8e5e1790caef0ade812aa13947c374c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
double factorial(int n){ | |
if(n < 1){ | |
return 1; | |
} else if(n == 1){ | |
return n; | |
} else { | |
return n * factorial(n - 1); | |
} | |
} | |
double accumulate(double sum, int n){ | |
int sign = n % 2 == 0 ? 1 : -1; | |
return sum + 1 / factorial(n) * sign; | |
} | |
double absDouble(double n){ | |
return n > 0 ? n : -n; | |
} | |
double fixedPoint(double lastTime, int n){ | |
double thisTime = accumulate(lastTime, n); | |
if(absDouble(thisTime - lastTime) <= 0.000001){ | |
return thisTime; | |
} else { | |
return fixedPoint(thisTime, n + 1); | |
} | |
} | |
double calc(){ | |
double num = 1.0; | |
double ans = 1.0; | |
double lastAns = 0.0; | |
for(int i = 1; absDouble(lastAns - ans) >= 0.000001 ; ++i){ | |
lastAns = ans; | |
num *= -1.0 / i; | |
ans += num; | |
} | |
return ans; | |
} | |
int main(){ | |
printf("%f\n", fixedPoint(0, 0)); | |
printf("%f\n", calc()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment