Thread: How to compute the natural logarithm of a factorial: large numbers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    19

    asymptotic expression if you don't need high precision

    Any math handbook or google can tell you the following formula for large N

    ln(N!) \approx [ N ln(N) ] -N

    Try that with your number.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    No, no! Please, don't use that approximation for statistical tests!

    If you want to implement a routine for other purposes, where you precision is not that important you can use it. If you need precision, however, (i.e. to compute exact P-values for some statistical tests), the best compromise between efficiency and accuracy is to use the gamma approximation:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int x;
    long double f;
    
    long double lnfactorial( int a);
    
    
    
    
    int main(void)
    {
    	
    		printf("Enter an integer :");
    		scanf("%d" , &x);
    				
    	f = lnfactorial(x);
    	
    	printf("\nThe ln factorial of %d is %9.12Lf\n", x, f);
    	
    	return 0;
    
    }
    
     long double lnfactorial( int a)
      {
    	  
    	long double z;
          if (a == 1)
              return 0;
          else
          {
    	      z = lgamma(a+1);
    	      
    	      return (z);
          }
      }
    The code above calculates the natural logarithm of n! for very large numbers of n using the gamma approximation. For me, (32-bits, linux) the maximum n can be 2147483646, since 2147483647 is the limit for a 32-bits system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Large Numbers
    By rkjd2 in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2002, 06:49 PM
  2. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  3. Very large floating point numbers
    By bulsquare in forum C Programming
    Replies: 0
    Last Post: 04-08-2002, 04:10 PM
  4. printing large decimal numbers
    By Isometric in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 09:55 PM
  5. commas in large numbers
    By HKR in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 07:08 PM