Last active
October 22, 2024 18:48
-
-
Save juanfal/8c7b827df2417ce271d25a4d06468847 to your computer and use it in GitHub Desktop.
exp n terms 1 call
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
// 01.exp.cpp | |
// juanfc 2024-10-22 | |
// calculate the factorial directly by adding n terms | |
// of its series development | |
// Look at https://en.wikipedia.org/wiki/Taylor_series | |
// https://gist.github.com/juanfal/8c7b827df2417ce271d25a4d06468847 | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
double exp(double x, int n=10); | |
for (double x = 1; x < 100; x+=10) | |
cout << "exp(" << x << ")= " << exp(x) << endl; | |
return 0; | |
} | |
double exp(double x, int n=10) | |
{ | |
double fact = 1; | |
double pot = 1; | |
double s = 0; | |
for (int i = 1; i <= n; ++i) { | |
s += pot/fact; | |
pot *= x; | |
fact *= i; | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment