A convenient approximation for computing the factorial n! for large valuse of n
ID: 3758932 • Letter: A
Question
A convenient approximation for computing the factorial n! for large valuse of n is given by the Stirling formula n! = (2**n)^(1/2)*(n/e)^n. Where e is the base for natural lograthims, or approximately 2.718282. Write an interger function for computing this approximation to a factorial. Assume the corresponding prototype is int n_fact(int n);
So far I've got;
//
// main.c
// Factorial
//
// Created by Leah Chamberlain on 10/30/15.
// Copyright © 2015 Leah Chamberlain. All rights reserved.
//
#include <stdio.h>
int factorial(int);
double e = 2.718282;
double pi = 3.1415926535;
int main(void){
int n;
printf("enter the number to calculate n! of ");
scanf("%d",&n);
if(n>=1)
printf("n!: %d! is %d ", n,factorial(n));
else
printf("enter an number greater than zero ");
return 0;
}
int n_fact(int n){
int result;
printf("-- %d ",n);
if(n==1)
result = 1;
else
result = pow(n, (n/e)) * sqrt(2*pi*n);
return result;
}
It's not working though
Explanation / Answer
In your program you mention values
double e=2.718282;
double pi=3.1415926535;
but your not mention
#include <math.h>
you didnot return that function.
below is my code
#include <stdio.h>
#include <math.h>
int factorial(int);
int main(){
int i,factorial,num;
printf("enter the number:");
scanf("%d",&num);
factorial=n_fact(num);
printf("Factorial of the %d is: %d",num,factorial);
return 0;
}
int n_fact(int num)
{
int i,f;
for(i=1;i<=num;i++)
f = pow(num, (num/e)) * sqrt(2*pi*num);
return f;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.