Tools · Math & Statistics

Factorial Calculator

n! for any whole number up to 1,000, with every digit computed exactly — not the scientific-notation approximation a pocket calculator gives up and returns. Along with it: how many digits the answer has, how many zeros it ends in, and where a 64-bit float would have started lying to you.

Exact big-integer arithmetic, every digitHow it worksInputs stay on this device
Your input

Multiply everything up to n

A whole number from 0 through 1,000. Every digit of the answer is computed exactly, however many there turn out to be — 1000! has 2,568 of them.

Your inputs are calculated locally and are not stored.
20!2432902008176640000

20! multiplies together every whole number from 1 to 20. The exact answer has 19 digits and ends in 4 zeros.

Digits
19
Trailing zeros
4
As a 64-bit float
2.432902e+18
Exact value
2432902008176640000
Formula & methodology

One product, and three things you can know about it without computing it

The factorial itself is the simplest definition in this whole toolkit: multiply together every whole number from 1 up to n. What makes it interesting is how fast it outgrows the machine, and how much you can say about the answer before you have it.

n! = 1 × 2 × … × n  •  n! = n × (n − 1)!  •  0! = 1
n!
The product of every whole number from 1 through n — 24 for n = 4
Digits
⌊n·log₁₀(n ÷ e) + ½·log₁₀(2πn)⌋ + 1, from Stirling's approximation
Trailing zeros
⌊n ÷ 5⌋ + ⌊n ÷ 25⌋ + ⌊n ÷ 125⌋ + … , Legendre's formula

Why 0! = 1

This is the question people actually arrive with, and the usual answer — “it is defined that way” — is unsatisfying because it sounds arbitrary. It is not. A factorial is a product, and the product of nothing at all is 1, exactly as the sum of nothing at all is 0: those are the values that leave the operation undisturbed when you fold in another term.

Two independent routes force the same answer. The recurrence n! = n × (n − 1)! evaluated at n = 1 says 1! = 1 × 0!, and 1! is obviously 1, so 0! must be 1. And the combination formula C(n, r) = n! ÷ (r! × (n − r)!) has to give 1 for C(n, 0), because there is exactly one way to choose nothing — which requires 0! in the denominator to be 1. Define 0! as 0 and every binomial coefficient divides by zero at its own boundary.

Where the digit count comes from

Stirling's approximation. Rather than count the digits of a number you have not built yet, take the base-10 logarithm of the product — which turns it into a sum — and approximate that sum with n·log₁₀(n ÷ e) + ½·log₁₀(2πn). The digit count is that value rounded down, plus one. For n = 1000 it gives 2567.6046, so 2,568 digits, which is exactly what the exact value turns out to have. The tool counts the digits off the real answer; Stirling is why you can know the size of that answer in advance.

The trailing zeros, without the factorial

This is a classic interview question and it deserves the reputation, because the elegant answer is genuinely short. A trailing zero is a factor of 10. Every 10 is a 2 paired with a 5. A factorial contains vastly more 2s than 5s — every second number contributes a 2, only every fifth contributes a 5 — so the supply of 5s is what limits the pairing, and counting the 5s counts the zeros.

Counting them is Legendre's formula: ⌊n ÷ 5⌋ multiples of 5, plus ⌊n ÷ 25⌋ because every multiple of 25 brings a second 5, plus ⌊n ÷ 125⌋ for the third, and so on until the divisor exceeds n. For n = 1000 that is 200 + 40 + 8 + 1 = 249 zeros — arrived at with four divisions, against the alternative of computing a 2,568-digit number and counting backwards from the end. This calculator does it the short way, which also means the zero count and the digits it prints are two independent computations that would disagree loudly if either were wrong.

Why the answer is a string, not a number

A 64-bit float can represent every whole number only up to 9,007,199,254,740,991, which 19! has already passed. Beyond that it holds a thinning subset of the integers, and 19! through 22! land on members of that subset purely because a factorial accumulates factors of two so quickly. At 23! the coincidence ends, and every larger factorial computed in floating point is wrong in its low digits while still looking perfectly authoritative. This engine accumulates the product in arbitrary-precision integers and hands back a decimal string, so the digits you see are the digits that are there.

Worked example

20!, all the way through

Twenty is the default above, and it is the most instructive input on the whole range because it sits exactly on the boundary. The product 1 × 2 × 3 × … × 20 comes to 2,432,902,008,176,640,000 — nineteen digits.

Check the digit count against Stirling before trusting it: 20 × log₁₀(20 ÷ 2.71828) + ½ × log₁₀(2π × 20) = 20 × 0.86636 + ½ × 2.09920 = 17.3272 + 1.0496 = 18.3843. Round down and add one: 19 digits. Correct.

And the zeros: ⌊20 ÷ 5⌋ = 4 multiples of five below twenty (5, 10, 15, 20), and ⌊20 ÷ 25⌋ = 0, so the total is 4 trailing zeros. Look at the last four digits of the answer above — 0000. Four, as promised, from two divisions.

Now the boundary. 2,432,902,008,176,640,000 is under 2⁶³ — 20! is the largest factorial that fits in a signed 64-bit integer — and a double represents it exactly. So do 21! and 22!, but only by luck: they carry enough factors of two to land on values the format happens to hold. At 23! the luck runs out. The true value is 25,852,016,738,884,976,640,000 and the closest double is 25,852,016,738,884,978,212,864 — adrift by 1,572,864, and wrong from the seventeenth digit on. Type 23 above and compare it with the “as a 64-bit float” line; that gap is the entire reason this page exists.

Push further and the two diverge completely. At n = 100 the exact answer has 158 digits and ends in 24 zeros; the float is 9.332622e+157, which is right to about sixteen significant figures and silent about the other 142. Above 170! the float gives up entirely and reports Infinity, while the exact value carries on — 1000! is 2,568 digits ending in 249 zeros.

Assumptions

What this calculator assumes

  • n is a whole number from 0 through 1,000. Fractions are refused rather than rounded — 4.5! is the gamma function Γ(5.5), a different question with a different answer.
  • Negative arguments have no factorial at all, so they are refused too. Running the recurrence backwards from 0! = 1 needs a division by zero at the first step.
  • The exact value is the answer. The floating-point figure is shown for comparison with other tools, not as a result to use; it is exact only through 22! and Infinity above 170!.
  • The digit count is counted off the exact value rather than estimated. Stirling is cited above as the reason the size is predictable, not as the source of the number shown.
  • The trailing-zero count comes from Legendre's formula and never touches the expanded product, which is what makes it a genuine cross-check rather than a restatement.
  • Very long values are collapsed in the headline — first fifty digits, last twenty, and the total in between. The complete value is in the breakdown below it and in the CSV and Excel exports.
Common questions

Factorial FAQ

Why is 0! equal to 1 and not 0?

Because n! is a product, and the product of no numbers at all is 1 — the same way the sum of no numbers is 0. That is the empty product convention, and it is not an arbitrary one: it is the only value that keeps the rest of the mathematics working. The recurrence n! = n × (n − 1)! at n = 1 reads 1! = 1 × 0!, and since 1! is plainly 1, 0! has to be 1. Combinations force the same answer: there is exactly one way to choose nothing from a set of n things, and the formula C(n, 0) = n! ÷ (0! × n!) only gives that answer if 0! is 1. Setting 0! to 0 would make every combination formula divide by zero at its boundary.

Why does my spreadsheet give a different answer for 23! than this page?

Because a spreadsheet computes in 64-bit floating point, which represents every whole number only up to 9,007,199,254,740,991 and a thinning subset above that. The exact value of 23! is 25852016738884976640000; the closest double is 25852016738884978212864, which differs from the seventeenth digit onwards. Curiously 19! through 22! do come out exactly right — a factorial accumulates factors of two fast enough to keep landing on representable values — and 23! is where that luck runs out. This page accumulates the product in arbitrary-precision integers, so every digit is the real one.

How many trailing zeros does 100! have, and how do you know without computing it?

Twenty-four, and you can get that in about ten seconds on paper. Every trailing zero is a factor of 10, every 10 is a 2 times a 5, and factorials contain far more 2s than 5s — so the number of 5s decides it. Count them with Legendre's formula: floor(100/5) = 20 multiples of 5, plus floor(100/25) = 4 more because 25, 50, 75 and 100 each bring a second 5, plus floor(100/125) = 0. Total 24. This calculator computes the zero count that way rather than reading it off the end of the answer, so the two are independent checks on each other.

How many digits does 1000! have?

2,568. The estimate comes from Stirling's approximation: log10(n!) is about n × log10(n ÷ e) + half of log10(2πn), and the digit count is that value rounded down plus one. For n = 1000 the expression gives 2567.6046, so 2,568 digits. The number shown on this page is counted off the exact value rather than estimated, but Stirling is why you can know the answer will be roughly 2,568 before computing anything.

What is the factorial of a negative or fractional number?

For fractions it is the gamma function, Γ(n + 1), which extends the factorial to every real and complex number except the negative integers — and it gives genuinely surprising answers, such as (1/2)! being half the square root of pi, about 0.8862. For negative integers there is no value at all: the recurrence n! = n × (n − 1)! run backwards from 0! = 1 requires dividing by zero at every step. This calculator refuses both rather than interpolating, because a factorial tool quietly returning gamma values is a factorial tool giving answers to a question you did not ask.

Why does this stop at 1,000?

Nothing mathematical stops there — the arbitrary-precision loop would happily keep going. It is a display limit. 1000! already has 2,568 digits, which is more than a result panel can usefully show, and the digit count roughly triples again by 3,000. If you need a larger factorial for a proof or a probability, the digit count and the trailing-zero count are usually the parts you actually want, and both can be computed by hand from the formulas above without expanding the product at all.

Primary sources

Sources and review notes

  1. NIST Digital Library of Mathematical Functions §5.2 — the gamma function and the factorial it extends, including Γ(n + 1) = n!
  2. NIST Digital Library of Mathematical Functions §5.11 — Stirling's series, the asymptotic expansion behind the digit count
  3. OEIS A000142 — the factorial sequence, with exact values to check any answer on this page against

The definition here is not the kind of thing that goes stale; what can go wrong is the arithmetic. The engine behind this page is a pure function under unit test, and the tests pin the exact digits of 20!, 25! and 1000! as well as the point at which the floating-point figure becomes Infinity.