MathsNumber Theory

Factorial
calculator.

Multiply every whole number from n down to 1, exactly, even with thousands of digits.

How it's worked out

The factorial of n, written n!n{!}, multiplies every whole number from n down to 1. It counts the ways to put n different things in a row, as OEIS A000142 records. Ten books fit on a shelf in 10!10{!} = 3,628,800 orders.

n!\displaystyle n{!}=n×(n−1)×⋯×2×1\displaystyle {}= n \times (n - 1) \times \cdots \times 2 \times 1

By convention 0!0{!} = 1. There is exactly one way to arrange nothing, and the value keeps n!n{!} = n × (n−1)!(n - 1){!} true when n is 1. The numbers grow very fast, so the tool keeps every digit up to 5,000!5{,}000{!}, which has 16,326 digits, and estimates the size beyond that.

Each zero at the end of n!n{!} needs a 10, which is a 2 times a 5. There are always more 2s than 5s, so you count the 5s. Every multiple of 5 adds one, every multiple of 25 adds another, and so on. OEIS A027868 gives the rule.

zeros\displaystyle \text{zeros}=⌊n5⌋+⌊n25⌋+⋯\displaystyle {}= \left\lfloor \frac{n}{5} \right\rfloor + \left\lfloor \frac{n}{25} \right\rfloor + \cdots

The same count works for any prime, which gives the whole prime factorization. For example, 2 divides 10!10{!} exactly 8 times. The prime table lists every prime up to n with its count.

Stirling's formula estimates n!n{!} without multiplying. It is the first term of the series in NIST DLMF 5.11, whose next factor is 1 + 1 over 12n, so the plain formula always comes out a little low.

n!\displaystyle n{!}≈2πn(ne)n\displaystyle {}\approx \sqrt{2\pi n}\,\left(\frac{n}{e}\right)^{n}

For decimals, DLMF 5.4 sets n!n{!} = Γ(n + 1), where Γ is the gamma function. It also gives Γ of one half as √π, so 0.5!0.5{!} = Γ(1.5) = 0.886227.

Two relatives use the same idea. The double factorial n!!n{!}{!} multiplies every other number, so 8!!8{!}{!} = 8 × 6 × 4 × 2 = 384 (OEIS A006882). The subfactorial !n{!}n counts orders that leave nothing in its own place (OEIS A000166).

Worked examples

Jordan counts the orders of a shuffled deck

A deck has 52 different cards, so it can be shuffled into 52!52{!} orders. Rounded to 5 significant figures, that is 8.0658 × 10⁶⁷, a number with 68 digits.

It ends in 12 zeros, one for each factor of 5 among the numbers up to 52. Stirling's formula gives 8.0529 × 10⁶⁷, which is 0.16% low.

Open this example in the tool

Amara's spreadsheet stops at 170 factorial

Amara's spreadsheet shows 170!170{!} but gives an error for 171!171{!}. The reason is the largest ordinary floating-point number, about 1.8 × 10³⁰⁸, given as Number.MAX_VALUE in the MDN reference.

170!170{!} is about 7.2574 × 10³⁰⁶, just under that limit. 171!171{!} is about 1.24 × 10³⁰⁹ and has 310 digits, so it does not fit. Here both are exact whole numbers, so 171!171{!} comes out in full.

Open this example in the tool

Four guests and a mixed-up hat check

Four guests leave their hats, and the attendant hands them back at random. There are 4!4{!} = 24 ways to hand them back. The subfactorial !4{!}4 = 9 counts the ways in which nobody gets their own hat.

So in 9 of the 24 orders everyone goes home with the wrong hat. With 10 guests the count is 1,334,961 out of 3,628,800. OEIS notes that !n{!}n is n!n{!} divided by e, rounded to the nearest whole number.

Open this example in the tool

Reading your result

Digits grow faster than you expect. 100!100{!} has 158 digits and 1,000!1{,}000{!} has 2,568. The digit count is the number to read when you only need the size.

Zeros come from 5s. 100!100{!} ends in 24 zeros and 1,000!1{,}000{!} in 249, both exact even though the tool only estimates the size past 5,000!5{,}000{!}.

Stirling gets better as n grows. For 1,000!1{,}000{!} it is low by 0.0083%, close to the 1 in 12n the series predicts.

\(n{!}{!}\) is not a factorial taken twice. 6!!6{!}{!} = 48, while (3!)!(3{!}){!} is 6!6{!} = 720. The double factorial is far smaller.

Going backwards. Type a value to find the n behind it: 3,628,800 is 10!10{!} exactly. A value that is not a factorial, such as 1,000,000, sits between 9!9{!} and 10!10{!}.

Negative decimals work, negative whole numbers do not. Γ of one half is √π, so (−0.5)!(-0.5){!} = 1.77245. At −1, −2 and the other negative whole numbers there is no value at all.

Common mistakes

  • Thinking \(0{!}\) is 0. It is 1, the empty product, and many formulas break without it.
  • Counting only the multiples of 5 for zeros. 25, 50, 75 and 100 each hold a second 5, which is why 100!100{!} ends in 24 zeros, not fewer.
  • Reading Stirling's value as exact. It is an estimate that is always a little low. Use the exact digits when you need the number itself.
  • Mixing up \(n{!}{!}\) and \((n{!}){!}\). The first multiplies every other number; the second takes the factorial of a factorial.

Questions people ask

There is exactly one way to arrange zero objects: do nothing. Setting 0 factorial to 1 also keeps the rule that n factorial is n times the factorial of n − 1 working at n = 1, and it makes the formula for choosing all or none of n items give 1.

Sources