1:      string factorial(int n)
   2:      {
   3:          var ex = 0.0;
   4:          var x = (double)n;
   5:          x = x + x + 1;
   6:   
   7:          if (x > 1)
   8:          {
   9:              x = (Math.log(2.0 * Math.Pi) + Math.log(x / 2.0) * x - x
  10:                - (1.0 - 7.0 / (30.0 * x * x)) / (6.0 * x)) / 2.0;
  11:              x = x / Math.log(10);
  12:              ex = Math.floor(x);
  13:              x = Math.pow(10, x - ex);
  14:          }
  15:   
  16:          var res = x.toString();
  17:          return res.substring(0, 6) + " E " + ex.toString();
  18:      }
  19:   
  20:      void calc()
  21:      {
  22:          Console.writeLine(factorial(134217728));
  23:      }

Clearly, this method has it's limitations, but it is amazingly powerful. The charm of the formula lies in the small integer coefficients: 2,1,7,30,6,2. Therefore it is also well suited for the use with a pocket calculator. (The order of the formula is O(n-5).) Some obsolete browsers may use a float instead of a double and run into floating point trouble.

Our small calculator computes 134217728! ~ 8.3459 E 1032606161 using this formula. A better approximation is 8.34597925944327661772882039785919428429176258950852382 E 1032606161.
This approximation can for example be calculated as described in this section
' A higher precision approximation'.

previous Back to the calculator.
Go to the RetroFactorial page to see how your parents calculated the factorial.  next
next Back to the Homepage of Factorial Algorithms.