Skip to content

Instantly share code, notes, and snippets.

@johnciacia
Created July 18, 2011 22:25
Show Gist options
  • Save johnciacia/1090849 to your computer and use it in GitHub Desktop.
Save johnciacia/1090849 to your computer and use it in GitHub Desktop.
Calculate the value of e as the sum of the infinite series
#include <stdio.h>
/**
* This code will calculate e
* as the sum of the infinite series
* 1/1! + 1/2! + 1/3! + ... + 1/n!
* g++ e.c
* ./a.out
*/
int factorial(unsigned int i);
int main() {
double e = 1.0;
for (int n = 10; n > 0; n--)
e += 1.0/factorial(n);
printf ("e = %lg\n", e);
return 0;
}
int factorial(unsigned int i) {
if(i == 0 || i == 1)
return 1.0;
return i * factorial(i-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment