-1

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.

Sarah H.
  • 3
  • 1
  • 1
  • 2

1 Answers1

2

OP code's performs weak or incorrect math in at least 2 places.

1 / (12 * i) is int math rather than the needed FP 1.0 / (12 * i).

Code "rounds" by passing through an int. Yet this, at best, truncates toward 0.0. Better to use double stirling_ans1; than int stirling_ans1;


Minor:
Unclear why code uses coarse approximations of pi and e. Perhaps:

#define PI 3.1415926535897932384626433832795
#define e 2.7182818284590452353602874713527

Or better yet, form values from a one-time calculation:

double pi = acos(-1.0);
double e = exp(1.0);
chux - Reinstate Monica
  • 113,725
  • 11
  • 107
  • 213