Arithmetic, Algebra & Number Theory Sequences, Series & Combinatorics Gamma function; Legendre's formula for trailing zeros

Factorial Calculator (n!)

The factorial of a whole number n, written n!, is the product of every whole number from 1 up to n. It counts the ways to arrange n distinct things in a row, which is why it sits underneath every permutation, combination and Taylor series you will ever meet. This calculator multiplies the digits out exactly rather than in floating point, so 100! and 1000! come back with every digit accounted for. It also reports how many digits the answer has, how many zeros it ends in, and the double factorial n!! — and it extends to non-integer inputs through the gamma function.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
nA whole number from 0 to 1000 for an exact answer; a decimal is evaluated through the gamma function instead.10
Which factorialThe double factorial skips every other term, so 9!! = 9 × 7 × 5 × 3 = 945. It is not the factorial applied twice.Ordinary factorial n! = n × (n−1) × … × 1

It returns

  • Value — Exact digits up to 30 of them; longer results are shown as an exact mantissa times a power of ten.
  • Number of digits — How long the exact answer is in base 10.
  • Trailing zeros — Zeros at the end of the exact value, counted from the digits themselves.
  • log₁₀ of the value — Useful for comparing factorials that are far too large to print.

The formula

n!=k=1nk=n(n1)21
Z=k=1n5k
n!2πn(ne)n

In plain text: n! = n × (n − 1) × (n − 2) × … × 2 × 1, with 0! = 1

  • n!Factorial of n — the number of ways to arrange n distinct objects in a row (count)
  • nA non-negative whole number (or any real number, via the gamma function) (count)
  • n!!Double factorial — the product of n, n−2, n−4, … down to 2 or 1 (count)
  • Γ(x)Gamma function; Γ(n + 1) = n! for whole numbers and interpolates smoothly between them (—)

0! = 1 is a definition, not an accident: it is the empty product, and it is the value that keeps the permutation and combination formulas consistent at their boundaries.

Updated Category Sequences, Series & Combinatorics Verified against published test cases Reading time 11 min

What a factorial counts

n! is the number of ways to put n distinct objects in order. Line up three books and there are 3! = 6 shelf orders; line up ten and there are 3,628,800. The reasoning is a chain of choices: n candidates for the first position, n − 1 still unused for the second, and so on until one remains. Multiply the choices and you have the factorial.

That interpretation is why factorials appear wherever arrangements are counted. The permutation calculator uses n!/(n−r)! for ordered selections; the combination calculator divides by r! to throw the ordering away. Factorials also sit in the denominators of the Taylor series for ex, sin x and cos x, in the Poisson and binomial probability formulas, and in the coefficients of Pascal's triangle.

The growth is unlike anything else you meet in elementary mathematics. Doubling is exponential and feels fast, but 220 is about a million while 20! is about 2.4 quintillion. Factorial growth outruns every fixed exponential eventually, because each new factor is larger than the last rather than constant. That is precisely why a brute-force search over orderings — the travelling-salesman route, a seating plan, a permutation cipher — becomes impossible somewhere around twenty items no matter how fast the computer.

How this calculator gets every digit right

Ordinary computer arithmetic cannot hold a large factorial. A 64-bit floating-point number represents integers exactly only up to 9,007,199,254,740,991, which 21! already exceeds, and it overflows to infinity entirely above 170!. So this page does not use floating-point multiplication for the value. It stores the running product as an array of seven-digit blocks and multiplies the whole array by each new factor with a carry, exactly the way you were taught long multiplication — which is why 1000! comes back with all 2,568 of its digits accounted for rather than as an overflow.

Three quantities are then read straight off that exact result. The digit count is the length of the string. The trailing zeros are counted from the end of it. And log₁₀ of the value is accumulated as a sum of logarithms while the product is built, which keeps it accurate even for inputs where the value itself is astronomically large.

The trailing-zero count has an elegant shortcut worth knowing, called Legendre's formula. A trailing zero comes from a factor of 10, and each 10 needs a 2 and a 5. Among the numbers 1 to n there are always more factors of 2 than of 5, so the count of fives decides the answer: ⌊n/5⌋ numbers contribute at least one five, ⌊n/25⌋ contribute a second, ⌊n/125⌋ a third, and so on. For n = 100 that is 20 + 4 = 24 zeros. The calculator shows this sum as a step and cross-checks it against the digits it actually produced.

For non-integer input the factorial is replaced by the gamma function, the unique smooth extension satisfying Γ(x + 1) = x·Γ(x) with Γ(1) = 1. It gives Γ(1.5) = √π/2 = 0.886227 for the famous "half factorial", and it is what makes expressions like the volume of an n-dimensional sphere or the density of the chi-square distribution well defined for odd dimensions.

Worked example: 10!, its zeros, and a Stirling check

Compute 10! by hand, then verify it three ways.

  1. Multiply upward. 1 × 2 = 2; × 3 = 6; × 4 = 24; × 5 = 120; × 6 = 720; × 7 = 5,040; × 8 = 40,320; × 9 = 362,880; × 10 = 3,628,800.
  2. Count the digits. 3,628,800 has 7 digits, and log₁₀(3,628,800) = 6.5598, so ⌊6.5598⌋ + 1 = 7 as expected.
  3. Predict the trailing zeros. Legendre's formula gives ⌊10/5⌋ = 2, and ⌊10/25⌋ = 0, so the total is 2. The value ends in "00" — correct.
  4. Check with Stirling. √(2π × 10) = √62.832 = 7.9267, and (10/e)10 = 3.67879410 = 454,000. Multiplying gives about 3,598,700, which is 0.83% below the true 3,628,800. Stirling's relative error is close to 1/(12n), and 1/120 = 0.833% — a match to two significant figures.

Now push the same machinery to 100!. Legendre's formula gives ⌊100/5⌋ + ⌊100/25⌋ = 20 + 4 = 24 trailing zeros. Summing log₁₀ of every factor from 2 to 100 gives 157.97000, so the value has ⌊157.97⌋ + 1 = 158 digits and begins 9.332621544 × 10157. Enter 100 above and all three figures appear without any of the digits being estimated.

How to read the result

Read the digit count first when the number is large. "158 digits" conveys the scale of 100! far better than 158 printed digits do, and it is the figure you actually need when you are deciding whether an algorithm is feasible or whether a data type will hold a value.

Some thresholds worth carrying in your head, each of them a hard boundary in real code. 12! = 479,001,600 is the largest factorial that fits in a signed 32-bit integer. 20! = 2,432,902,008,176,640,000 is the largest that fits in a signed 64-bit integer, and 21! overflows it. 18! is the largest that a double-precision float still represents exactly, because 19! exceeds 253. 170! ≈ 7.26 × 10306 is the largest that a double can hold at all; 171! returns infinity in almost every language that uses IEEE 754. Any of these will silently corrupt a calculation if you cross it in the wrong type.

The trailing-zero count answers a class of contest and interview question by itself, and it also tells you the largest power of 10 that divides n!. Because it grows like n/4 rather than n, roughly a quarter of the digits at the end of a large factorial are zeros: 1000! ends in 249 zeros, which you can verify with ⌊1000/5⌋ + ⌊1000/25⌋ + ⌊1000/125⌋ + ⌊1000/625⌋ = 200 + 40 + 8 + 1.

If you need to compare two enormous factorials, compare the log₁₀ values rather than the numbers. The difference between the logs is the log of the ratio, so a gap of 3 means one is a thousand times the other. That trick is also what keeps the combination calculator honest for large binomial coefficients, where the factorials involved would overflow long before their quotient does.

Factorials from 0! to 20!

Exact values, digit counts and trailing zeros. 20! is the last entry that fits in a signed 64-bit integer.
nn!DigitsTrailing zeros
0110
1110
2210
3610
42420
512031
672031
75,04041
840,32051
9362,88061
103,628,80072
1139,916,80082
12479,001,60092
136,227,020,800102
1487,178,291,200112
151,307,674,368,000133
1620,922,789,888,000143
17355,687,428,096,000153
186,402,373,705,728,000163
19121,645,100,408,832,000183
202,432,902,008,176,640,000194

Notice that the digit count sometimes jumps by two (14 to 15, 18 to 19) and sometimes not at all — the increase is ⌊log₁₀ n⌋ rounded by where the leading digits happen to fall.

Traps and misconceptions

  • Thinking 0! = 0. It is 1. The empty product is 1 the same way an empty sum is 0, and any other value would break C(n, n) = 1 and the series for ex.
  • Reading n!! as (n!)!. The double factorial skips alternate terms — 9!! = 945 — while (3!)! = 720. Two exclamation marks never mean apply the operation twice.
  • Asking for the factorial of a negative whole number. It does not exist. The gamma function has poles at 0, −1, −2 and every negative integer, so Γ(x) runs to infinity there; only non-integer negatives have a finite value.
  • Letting a language overflow silently. 21! wraps around in a 64-bit signed integer and 171! becomes infinity in a double. If your factorial is unexpectedly negative or infinite, you have crossed a type boundary rather than made an arithmetic error.
  • Computing n! to get a binomial coefficient. C(52, 5) is under three million, yet 52! has 68 digits. Cancel the factorials symbolically first, or use the multiplicative recurrence.
  • Counting trailing zeros with twos. The factors of 5 are always the scarcer of the pair, so only fives are counted. Using twos gives a wildly too-large answer.
  • Assuming Stirling's approximation is close enough. Its relative error is about 1/(12n), so it is 8% out at n = 1 and 0.83% out at n = 10. Useful for magnitude, not for exact digits.

Double factorials, gamma and the near neighbours

Several relatives of the factorial turn up often enough to be worth naming.

The double factorial n!! multiplies every second term: 8!! = 8 × 6 × 4 × 2 = 384 and 9!! = 9 × 7 × 5 × 3 = 945. It appears in the closed form for integrals of powers of sine and cosine, and in the volume of an even-dimensional sphere. The two are linked by (2k)!! = 2k · k! and (2k)! = (2k)!! × (2k−1)!!, which is a useful identity when a formula hands you one and you want the other.

The gamma function is the continuous version, and it is what lets a factorial appear in the density of a chi-square or t distribution where the degrees of freedom may be odd. Γ(n + 1) = n! at every whole number, Γ(1/2) = √π, and the reflection formula Γ(x)Γ(1 − x) = π/sin(πx) ties values on either side of the poles together.

The falling factorial n(n−1)…(n−r+1) — sometimes written n(r) or (n)r — is the piece of n! that survives when you divide by (n − r)!. It is the count of ordered selections, and it is what you should actually compute when a formula appears to demand two huge factorials.

Finally, factorials appear in series in a way that is easy to check yourself. e = 1 + 1/1! + 1/2! + 1/3! + … converges to 2.718282 after only eight terms because the denominators grow so fast. If you want to see that growth plotted against ordinary exponential growth, the exponential growth calculator and the scientific notation calculator are the tools for handling the magnitudes involved.

Key terms

Factorial
n! — the product of all whole numbers from 1 to n, and the number of orderings of n distinct objects. 0! = 1.
Double factorial
n!! — the product of n, n−2, n−4 and so on down to 2 or 1. Not the factorial applied twice.
Gamma function
Γ(x), the smooth extension of the factorial to real and complex arguments, with Γ(n + 1) = n! for whole n.
Legendre's formula
The count of a prime p in the factorisation of n!, equal to ⌊n/p⌋ + ⌊n/p²⌋ + …. With p = 5 it gives the trailing zeros.
Stirling's approximation
n! ≈ √(2πn)(n/e)ⁿ. Excellent for magnitude at large n, with relative error near 1/(12n).
Empty product
The product of no factors at all, defined as 1 so that multiplying by it changes nothing. This is what makes 0! = 1.

Frequently asked questions

Why is 0! equal to 1 and not 0?

Because 0! is an empty product, and an empty product is 1. There is exactly one way to arrange no objects — do nothing — so the counting interpretation gives 1 as well. The definition is also forced by the recurrence n! = n × (n−1)!: setting n = 1 gives 1! = 1 × 0!, and since 1! = 1 the only consistent value for 0! is 1. Any other choice would break C(n, 0) = 1 and the series for e.

What is 100 factorial?

100! is a 158-digit number beginning 9.332621544 × 10157 and ending in 24 zeros. Enter 100 above to see the exact digit string. It is far larger than any physical count you could name — current estimates of the number of atoms in the observable universe are around 1080, so 100! exceeds that by roughly 77 orders of magnitude.

Can you take the factorial of a decimal or a fraction?

Yes, through the gamma function. The factorial of x is defined as Γ(x + 1), which agrees with the ordinary factorial at every whole number and interpolates smoothly between them. The best-known value is 0.5! = Γ(1.5) = √π/2 = 0.886227. Enter any decimal above and the calculator switches to the gamma route automatically, and tells you it has done so.

How many trailing zeros does 1000! have?

249. Apply Legendre's formula with p = 5: ⌊1000/5⌋ + ⌊1000/25⌋ + ⌊1000/125⌋ + ⌊1000/625⌋ = 200 + 40 + 8 + 1 = 249. The terms stop there because 55 = 3125 exceeds 1000. Counting fives rather than twos is the key step — factors of 2 are always more plentiful, so the fives limit how many tens can be formed.

What is the largest factorial a computer can hold?

It depends entirely on the type. A signed 32-bit integer stops at 12! = 479,001,600; a signed 64-bit integer stops at 20!; a double-precision float represents factorials exactly only to 18! and overflows to infinity above 170!. Beyond that you need arbitrary-precision integers — Python's math.factorial, Java's BigInteger, or the exact block multiplication this page uses.

What does the double factorial mean?

n!! multiplies every second number down from n: 9!! = 9 × 7 × 5 × 3 = 945, and 8!! = 8 × 6 × 4 × 2 = 384. It is not the factorial of the factorial. Odd and even values behave differently, which is why (2k)!! = 2kk! while the odd case has no such clean form. Select the double factorial option above to compute it.

How do I calculate a factorial on a scientific calculator?

Enter the number, then press the x! key — usually a shifted function near the probability menu. On a TI-83/84 the path is MATH → PRB → ! ; on Casio models it is OPTN → PROB → x!. Most handheld calculators overflow above 69!, because their display tops out at 10100. In spreadsheets use =FACT(10), and =FACTDOUBLE(9) for the double factorial.

Why does factorial growth matter in computing?

Because an algorithm that examines every ordering of its input becomes unusable at a very small size. With 10 items there are 3,628,800 orderings, which a laptop clears instantly; with 20 there are 2.4 × 1018, which it will not finish in a human lifetime. That cliff between 10 and 20 is why exact solutions to routing and scheduling problems rely on pruning or heuristics rather than enumeration.

References