How To Do Factorial On Excel

12 min read

Calculating a factorial is a fundamental mathematical operation often required in statistics, combinatorics, and engineering. So microsoft Excel provides a dedicated, highly efficient function to handle this instantly. While the concept is straightforward—multiplying a sequence of descending natural numbers—performing this calculation manually for large numbers is impractical. Understanding how to do factorial on Excel unlocks the ability to solve complex permutation and probability problems directly within your spreadsheets without needing external calculators or programming scripts.

The primary tool for this task is the FACT function. Still, it is categorized under Math & Trig functions and is designed specifically to return the factorial of a given number. The syntax is remarkably simple: =FACT(number). So the argument number represents the non-negative integer for which you want the factorial. That said, if the number is not an integer, Excel truncates the decimal portion before calculating. This native function eliminates the need for recursive formulas or VBA macros for standard use cases, making it accessible to users of all skill levels.

Understanding the FACT Function Syntax and Rules

Before applying the function, it is crucial to understand the boundaries and rules that govern its behavior. The FACT function accepts a single argument, but that argument must adhere to specific constraints to return a valid result Less friction, more output..

  • Non-negative Integers Only: The function calculates the factorial for integers greater than or equal to zero. By mathematical definition, 0! = 1, and Excel honors this convention perfectly. Typing =FACT(0) returns 1.
  • Truncation of Decimals: If you supply a decimal value, such as 5.9, Excel does not round it; it truncates it to 5. The result will be 120 (5!), not 720 (6!).
  • Negative Numbers Return Errors: Factorials are undefined for negative integers. If you enter =FACT(-3), Excel will return a #NUM! error.
  • Upper Limit Constraints: This is the most critical practical limitation. Excel calculates factorials up to 170. The result of 170! is approximately 7.25741562 × 10^306, which is near the maximum positive number Excel can store (9.999 × 10^307). Attempting =FACT(171) or higher results in a #NUM! error because the result exceeds the application's numeric precision limit.

Step-by-Step Guide to Using the FACT Function

Implementing the factorial calculation in your worksheet takes only a few seconds. Here is the standard workflow:

  1. Select the Output Cell: Click on the cell where you want the factorial result to appear.
  2. Initiate the Formula: Type an equals sign = followed by FACT(. You can also type =FACT( directly or use the Insert Function (fx) button next to the formula bar, search for "FACT," and select it.
  3. Enter the Argument: You have three main ways to provide the number:
    • Hardcoded Value: Type the integer directly inside the parentheses. Example: =FACT(5).
    • Cell Reference: Click on a cell containing the number (e.g., A1) or type the reference. Example: =FACT(A1). This is the preferred method for dynamic datasets.
    • Formula Result: Nest another formula inside. Example: =FACT(SUM(A1:A3)).
  4. Close and Execute: Close the parenthesis ) and press Enter. The result will populate the cell immediately.

Practical Example: Calculating Permutations

A classic use case for factorials is calculating permutations—the number of ways to arrange r items from a set of n items where order matters. This leads to the formula is n! / (n-r)!.

Imagine you have 10 distinct books (n=10 in cell B1) and you want to know how many ways you can arrange 3 of them on a shelf (r=3 in cell B2).

  1. In cell B4, enter the formula: =FACT(B1) / FACT(B1 - B2).
  2. Press Enter.
  3. The result is 720.

This demonstrates how the FACT function serves as a building block for more complex statistical analysis directly within the grid Practical, not theoretical..

Handling Large Factorials: The 170 Limit and Workarounds

As noted earlier, the hard limit of 170!Which means if your workflow requires factorials beyond this threshold—such as 200! On top of that, —standard Excel arithmetic fails. Consider this: or 500! stops many scientific or cryptographic calculations in their tracks. Still, there are three viable workarounds depending on your specific needs.

1. Using the GAMMA Function (For Real Numbers)

The Gamma function extends the factorial concept to complex and real numbers. For positive integers, the relationship is Γ(n) = (n-1)!. Because of this, n! = Γ(n+1). Excel has a GAMMA function (available in Excel 2013 and later) Simple, but easy to overlook..

  • Formula: =GAMMA(171) calculates 170!.
  • Formula: =GAMMA(172) calculates 171!.
  • Limit: GAMMA also overflows around 171.624, so it only buys you a tiny margin over FACT.

2. Logarithmic Transformation (For Magnitude Comparison)

Often in statistics (like likelihood estimation), you do not need the actual factorial number; you need the natural logarithm of the factorial (ln(n!)). This allows you to work with massive numbers (like 1,000,000!) without overflow because you are summing logs instead of multiplying integers And that's really what it comes down to..

  • Excel Function: =GAMMALN(number + 1).
  • Example: =GAMMALN(1001) returns the natural log of 1000! (~5912). This is extremely fast and handles massive inputs effortlessly.

3. Custom VBA Function with Big Integer Libraries (For Exact Digits)

If you require the exact integer value of 200! (all 375 digits), you must use VBA (Visual Basic for Applications) with a "Big Integer" implementation, as native Excel types (Double, Long, Variant) cannot hold that precision. This involves writing a custom User Defined Function (UDF) that stores digits in arrays or strings and performs multiplication manually. This is an advanced developer approach but is the only way to get exact digits for n > 170 inside the Excel environment Simple, but easy to overlook..

The FACTDOUBLE Function: A Specialized Sibling

Excel includes a second factorial-related function: FACTDOUBLE. And this calculates the double factorial (denoted as n!! ), which is distinct from (n!)!.

  • For Even n: n!! = n × (n-2) × (n-4) × ... × 2
  • For Odd n: n!! = n × (n-2) × (n-4) × ... × 1

Syntax: =FACTDOUBLE(number)

Example:

  • =FACTDOUBLE(6) calculates 6 × 4 × 2 = 48.
  • =FACTDOUBLE(7) calculates 7 × 5 × 3 × 1 = 105.

This function is frequently used in specific engineering contexts, such as calculating the

number of ways to arrange objects into paired groupings, such as matching problems or certain probability calculations involving repeated pairings.

While the FACTDOUBLE function is specialized, it is still wise to test its behavior with extremely large inputs, as Excel’s underlying numeric limits can affect even some non-standard factorial calculations.

With these alternatives in hand, the 170! barrier becomes less of a hard stop and more of a prompt to choose the right mathematical approach. If exact integer output is essential, native Excel

Extending the Reach of Factorials Beyond the 170‑Digit Ceiling

When the need arises for an exact integer representation of a factorial that exceeds the 170‑digit ceiling, the most reliable path inside Excel is to lean on a custom VBA routine that implements arbitrary‑precision arithmetic. Below is a concise outline of how such a routine can be built and integrated, followed by a few complementary strategies for specific use‑cases And that's really what it comes down to..

1. A VBA‑Based Big‑Integer Factorial UDF

The core idea is to store each digit of the intermediate result in a dynamic array (or string) and perform the multiplication step‑by‑step, exactly as you would on paper. A minimal implementation looks like this:

Public Function FACTBIG(n As Long) As String
    Dim i As Long, carry As Long, idx As Long
    Dim result() As Long
    
    ' Initialise result with 1 (represented as a single digit)
    ReDim result(0 To 0)
    result(0) = 1
    idx = 1
    
    For i = 2 To n
        carry = 0
        ' Multiply each digit by i, propagating the carry
        For idx = LBound(result) To UBound(result)
            Dim temp As Long
            temp = result(idx) * i + carry
            result(idx) = temp Mod 10
            carry = temp \ 10
        Next idx
        
        ' Append remaining carry digits
        While carry > 0
            idx = idx + 1
            ReDim Preserve result(0 To idx)
            result(idx) = carry Mod 10
            carry = carry \ 10
        Wend
    Next i
    
    ' Convert the digit array to a readable string (least‑significant digit first)
    FACTBIG = ""
    For idx = UBound(result) To LBound(result) Step -1
        FACTBIG = FACTBIG & result(idx)
    Next idx
End Function
  • Advantages – The function returns the full factorial as a text string, guaranteeing that every digit is preserved regardless of size.
  • Limitations – Because VBA works with 16‑bit Long indices, the practical upper bound is set by available memory; in typical workbooks, factorials up to several thousand are computed instantly, while tens of thousands may require a few seconds.

To make the function accessible directly in worksheet cells, simply paste the code into a standard module and then use =FACTBIG(200) to obtain the 375‑digit result for 200! Most people skip this — try not to..

2. When Only the Magnitude Matters: Logarithms and Approximations

For many statistical and probabilistic calculations, the exact integer is unnecessary; the natural logarithm of the factorial (ln(n!)) suffices. Excel’s built‑in GAMMALN function already implements a highly accurate approximation:

=GAMMALN(201)   // ln(200!)

The result can be converted back to a base‑10 logarithm with =LOG10(EXP(GAMMALN(201))) if a sense of order of magnitude is required. For extremely large arguments (e.g., n > 10⁶), Stirling’s approximation—`ln(n!

  • n + 0.Plus, 5·ln(2πn). This approximation is remarkably accurate even for relatively modest values of n, and its error becomes negligible as n grows. For n > 10⁶, the computational savings are substantial because you avoid the iterative digit‑by‑digit multiplication entirely, relying instead on a closed‑form expression that Excel can evaluate in milliseconds.

Some disagree here. Fair enough.

When the factorial appears inside a ratio—such as a binomial coefficient, a Poisson probability, or a beta function—the logarithm is often the more natural and numerically stable path

3. Implementing Stirling’s Approximation Directly in Excel

When the argument of the factorial exceeds a few thousand, the digit‑by‑digit routine begins to consume noticeable CPU time. A closed‑form expression that captures the asymptotic behaviour of n! eliminates the need for an explicit loop altogether.

[ \ln (n!) ;\approx; n\ln n ;-; n ;+; \tfrac{1}{2}\ln (2\pi n) ]

Because Excel already provides the natural‑logarithm (LN) and the constant π, the approximation can be expressed as a single worksheet formula. For a cell A1 that contains the integer n, the following expression returns an approximation of n! (in logarithmic form):

=EXP( A1*LN(A1) - A1 + 0.5*LN(2*PI()*A1) )

If the actual value of n! Which means for n > 10⁶ the relative error drops below 0. is required as a text string, the same formula can be wrapped in TEXT‑style concatenation, but the exponential step already yields a number that is accurate to better than one part in 10⁴ for n ≥ 10⁴. 01 %, making the approximation effectively exact for most practical purposes while requiring only a few milliseconds of calculation time Not complicated — just consistent. Nothing fancy..

3.1 A Compact VBA Helper

For users who prefer to keep the calculation inside a custom function, a lightweight wrapper around Stirling’s formula can be added to the same module that houses FACTBIG. The routine below accepts a Long argument (or a Double for very large inputs) and returns a Double representing the approximated factorial:

Function FACTSTIRL(n As Double) As Double
    If n < 0 Then
        FACTSTIRL = CVErr(xlErrNum)
        Exit Function
    End If
    FACTSTIRL = Exp(n * Log(n) - n + 0.5 * Log(2 * WorksheetFunction.Pi() * n))
End Function

Because the return type is Double, the function can handle values up to roughly 1.7 × 10³⁰⁸, far beyond the range of the digit‑array method, while still delivering a result that is sufficient for ratio‑based calculations.

4. Using Log‑Factorials for Ratio‑Heavy Problems

Many combinatorial expressions—binomial coefficients, Poisson probabilities, beta‑function values—contain factorials in both numerator and denominator. Computing the raw factorials and then dividing quickly overflows even the 64‑bit floating‑point representation used by VBA. The standard remedy is to work entirely in the logarithmic domain:

[ \ln \binom{n}{k} ;=; \ln (n!) ;-; \ln (k!) ;-; \ln ((n-k)!

Excel’s GAMMALN already supplies a high‑precision approximation of ln Γ(z), and because Γ(n + 1) = n! we can write

=GAMMALN(n+1) - GAMMALN(k+1) - GAMMALN((n-k)+1)

The result is the natural logarithm of the binomial coefficient. Exponentiating the outcome (=EXP(...)) restores the actual coefficient when the value remains within the representable range; otherwise the log

-value is retained for further calculation to prevent overflow.

4.1 Implementing Log-Ratios in VBA

To integrate this logic into a programmatic workflow, one can create a function that calculates the binomial coefficient without ever computing the raw factorial. This is essential for calculating probabilities in large-scale statistical models where $n$ might be in the thousands That alone is useful..

Function LOGBINOM(n As Double, k As Double) As Double
    If k < 0 Or k > n Then
        LOGBINOM = -1 ' Or handle as an error
        Exit Function
    End If
    LOGBINOM = WorksheetFunction.GammaLn(n + 1) - _
               WorksheetFunction.GammaLn(k + 1) - _
               WorksheetFunction.GammaLn(n - k + 1)
End Function

By returning the logarithm, the developer can perform multiplications (which become additions) and divisions (which become subtractions) in the log-domain, only converting back to a standard number at the final step of the process.

5. Comparative Summary of Methods

Choosing the correct approach depends entirely on the required precision and the magnitude of the input $n$. The following table summarizes the three methods discussed:

Method Precision Range Limit Best Use Case
Standard FACT Exact $n \le 170$ Small integers, quick checks.
Digit-Array (VBA) Exact Memory limited High-precision accounting, cryptography.
Stirling/Gamma Approximate $\approx 10^{308}$ Statistical distributions, large-scale combinatorics.

Conclusion

Calculating factorials in Excel requires a strategic approach to avoid the "$#\text{NUM}!Consider this: ${content}quot; error associated with floating-point overflow. While the built-in FACT function is sufficient for basic tasks, professional-grade models often require the precision of a custom digit-array VBA function or the efficiency of Stirling’s approximation Easy to understand, harder to ignore..

By leveraging the GAMMALN function and working within the logarithmic domain, users can bypass the physical limits of 64-bit architecture, enabling the calculation of binomial coefficients and probabilities that would otherwise be computationally impossible. Whether seeking the absolute precision of an arbitrary-precision integer or the rapid estimation of a transcendental approximation, these tools check that Excel remains a viable environment for complex combinatorial analysis.

Right Off the Press

New Content Alert

If You're Into This

You Might Also Like

Thank you for reading about How To Do Factorial On Excel. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home