Stirling's approximation in c language -


i'm trying write code in c calculate accurate of stirling's approximation 1 12.

here's code:

#define pi 3.1416 #define eulernum 2.71828  float stirling_approximation(int n) {     int fact;     float stirling, ans;      fact = factorial(n);     stirling = sqrt(2.0*pi*n) * pow(n / eulernum, n);     ans = fact / stirling;      return ans; }  int factorial(int input) {     int i;     int ans = 0;      (i = 1; <= input; i++)         ans += i;     return ans; }  int main(void) {     int n;     printf(" n\t ratio\n");     (n = 1; n <= 12; n++) {         printf("n: %2d\t %f\n", n, stirling_approximation(n));     }      return 0; } 

i'm getting recursive calculation correctly, stirling's approximation method value way off. , what's more puzzling answers n = 1, 3 correct.

i think has calling approximation function main function. know must such simple mistake i've been trying fix entire day! please me?

thank you!

you incorrectly implemented factorial method

int factorial(int input) {     int i;     int ans = 0;      (i = 1; <= input; i++)         ans += i;     return ans; } 

it should be

int factorial(int input) {     int i;     int ans = 1;      (i = 2; <= input; i++)         ans *= i;     return ans; } 

i change return value int long int.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -