Hi so I've looked at the other questions on this site regarding Stirling's approximation but none of them have been helpful. I am suppose to be computing the factorial and also approximating the factorial from the two Stirling's approximation equations. Then I put them in a table of all values leading up to the user's input if the input is less than or equal to 14. Or I put them in a different table of all of the values of the approximations leading up to the user's input in multiples of 5. I've tried many things but I don't know where I've gone wrong.
I'm just wondering how to get the correct values for the approximations? I've already gotten the factorial.
Here's the entire code:
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
#define e 2.71828
#define TRUE 1
#define FALSE 0
long long fact(int i);
double stirling1(int i);
double stirling2(int i);
/*--------------------------- main function -------------------------------
Purpose: The purpose of this program is to give the user the value of the
factorial of the nonnegative integer the user inputs.
---------------------------------------------------------------------------*/
int main ()
{
char ch = 'y';
int flag, again;
int n;
int i;
again = TRUE;
while ( again == TRUE )
{
printf("Please enter a nonnegative integer:\n"); //ask the user for the input
flag = scanf("%d", &n);
if ( flag != 1 )
{
while ((getchar()) != '\n');
}
if ( flag != 1 ) //run this statement if the user inputs a noninteger
{
printf("Input must be an integer.\n");
continue;
}
else if ( n < 0 ) //run this statement if the user inputs a negative integer
{
printf("The factorial is undefined for negative arguments.\n");
continue;
}
else if ( n <= 14 ) //run this statement if the user inputs an integer less than or equal to 14
{
printf("Number Factorial Approximation Approximation2\n------------------------------------------------------------------\n"); //prints the header for first table
for ( i = 1; i <= n; ++i )
{
printf("%d %14lld %e %e\n", i, fact( i ), stirling1( i ), stirling2( i )); //calls functions to input factorials
}
}
else //run this statement if the user inputs a number greater than 14
{
printf("Number Approximation1 Approximation2\n-----------------------------------------------------\n"); //prints the header for second table
for ( i = 1; i != n; ++i )
{
i *= 5;
printf("%d %e %e\n", i, stirling1( i ), stirling2( i )); //calls functions to input approximate factorials
}
}
printf("Do you want to compute another factorial? (y/n):"); //ask user if they want another factorial of a different number
scanf(" %c", &ch);
if (ch != 'y')
again = FALSE; //if user does not input 'y' then do not compute another factorial
}
printf( "**** Program Terminated ****\n" ); //ends program
}
long long fact( int i ) //function to find exact factorial
{
if (i <= 1)
{
return 1;
}
else
{
return i * fact(i-1);
} //return exact factorial to main
}
double stirling1( int i ) //function to find first approximate factorial
{
int stirling_ans1;
stirling_ans1 = pow( i , i ) * sqrt( 2.0 * PI * i) * exp(-i); //equation to find first approximate factorial
return stirling_ans1; //return approximate factorial to main
}
double stirling2( int i ) //function to find second approximate factorial
{
int stirling_ans2;
stirling_ans2 = pow( i, i ) * pow( e, -i ) * sqrt( 2 * PI * i) * ( 1 + ( 1 / (12 * i) ));
//equation to find second approximate factorial
return stirling_ans2; //return approximate factorial to main
}
Here is the code for the two approximation functions specifically:
double stirling1( int i ) //function to find first approximate factorial
{
int stirling_ans1;
stirling_ans1 = pow( i , i ) * sqrt( 2.0 * PI * i) * exp(-i); //equation to find first approximate factorial
return stirling_ans1; //return approximate factorial to main
}
double stirling2( int i ) //function to find second approximate factorial
{
int stirling_ans2;
stirling_ans2 = pow( i, i ) * pow( e, -i ) * sqrt( 2 * PI * i) * ( 1 + ( 1 / (12 * i) ));
//equation to find second approximate factorial
return stirling_ans2; //return approximate factorial to main
}
And where the approximation functions were implemented in the main function:
else if ( n <= 14 ) //run this statement if the user inputs an integer less than or equal to 14
{
printf("Number Factorial Approximation Approximation2\n------------------------------------------------------------------\n"); //prints the header for first table
for ( i = 1; i <= n; ++i )
{
printf("%d %14lld %e %e\n", i, fact( i ), stirling1( i ), stirling2( i )); //calls functions to input factorials
}
}
else //run this statement if the user inputs a number greater than 14
{
printf("Number Approximation1 Approximation2\n-----------------------------------------------------\n"); //prints the header for second table
for ( i = 1; i != n; ++i )
{
i *= 5;
printf("%d %e %e\n", i, stirling1( i ), stirling2( i )); //calls functions to input approximate factorials
}
Any help to get the correct approximations would be appreciated.