Factorial Calculator
Find n! for any whole number from 0 to 170, plus how many digits it has and its natural log ln(n!). Covers why 0! = 1 and how Stirling's formula works.
Factorial Calculator
Background.
Ten people can stand in a queue in 3,628,800 different orders, which happens to be exactly the number of seconds in six weeks. That figure is 10!, read "ten factorial": 10 × 9 × 8 × … × 2 × 1. Whoever goes first can be any of the ten, the second any of the nine who are left, and so on down to the last person, who gets no choice at all. Multiplying the choices together is all a factorial is.
Factorials grow faster than almost anything else in everyday arithmetic. 5! is 120, 10! passes three and a half million, 20! is 2,432,902,008,176,640,000, and 52!, the number of ways to order a standard pack of cards, is about 8.066 × 10⁶⁷. Going from 10 to 20 multiplies the answer by roughly 670 billion. That speed is why factorials turn up in the denominators of probability formulas, binomial coefficients and the series used to compute eˣ, sine and cosine: they shrink terms faster than any power can inflate them.
Enter a whole number from 0 to 170 and you get three results: n! itself, the number of decimal digits it has, and its natural logarithm, ln(n!). The second and third exist because the first becomes unwieldy quickly. From 18! upward the value is shown in scientific notation to four significant figures, so the digit count gives the full size at a glance, and ln(n!) is the form statisticians actually work with when factorials sit inside a likelihood.
The ceiling of 170 has a reason. 170! is about 7.257 × 10³⁰⁶, while 171! is about 1.241 × 10³⁰⁹, beyond the largest value (about 1.798 × 10³⁰⁸) that standard double-precision floating point can hold. That is the number format behind spreadsheets and most programming languages, which is why Excel's FACT function also gives up at 171. Fractions and negative numbers are refused too: as a product, the factorial exists only for non-negative integers, and stretching it to other values needs the gamma function.
What is factorial calculator?
For a non-negative integer n, the factorial n! is the product of every positive integer from 1 up to n. For n = 0 through 10 the values run 1, 1, 2, 6, 24, 120, 720, 5,040, 40,320, 362,880 and 3,628,800. Each term is the previous one multiplied by n, so n! = n × (n − 1)!, and that rule plus the starting value 0! = 1 defines the entire sequence. In counting problems n! is the number of ways to arrange n distinct objects in a row, which is why it is also called the number of permutations of n items. Setting 0! = 1 is not a fudge: a product of no numbers equals 1 for the same reason a sum of no numbers equals 0, and there is exactly one way to arrange nothing. Outside the integers the factorial is carried on by the gamma function, which satisfies Γ(n + 1) = n! at every whole number.
How to use this calculator.
- Type a whole number from 0 to 170 into Integer n. If your real question is a ratio such as 52! ÷ (5! × 47!), you seldom need each factorial on its own; a permutation and combination calculator cancels the shared factors first, which is far more accurate than dividing two large numbers that have already been cut to four significant figures.
- Read n! in the first result. Up to 17! (355,687,428,096,000) every digit is printed; from 18! on, the value appears as four significant figures times a power of ten, so 20! shows as 2.433 × 10¹⁸.
- Use the digit count to judge size, for instance to decide whether a value fits a database column: 25! has 26 digits, 100! has 158 and 170! has 307. To see every digit, use a language with unlimited-size integers, such as Python's math.factorial, which prints them all.
- Take ln(n!) when the factorial is one piece of a bigger formula. Work in logarithms — ln C(n, k) = ln n! − ln k! − ln (n − k)! — and convert back with eˣ only at the end, so no intermediate value ever overflows.
The formula.
The definition is a running product, n! = 1 × 2 × 3 × … × n with 0! = 1, and the calculator multiplies it out in full using whole-number arithmetic, so nothing is approximated before the finished value is displayed.
The digit count comes from logarithms. A positive whole number N has ⌊log₁₀ N⌋ + 1 digits: log₁₀ 120 ≈ 2.0792, the floor is 2, add one and you get 3. Since log₁₀(n!) = ln(n!) ÷ ln 10, the digit count is read off the third result.
That third result, ln(n!), is found in one of two ways. Up to n = 100 it is the plain sum ln 2 + ln 3 + … + ln n. Above 100 it uses Stirling's series, ln(n!) ≈ n ln n − n + ½ ln(2πn) + 1/(12n) − 1/(360n³), as given in Abramowitz and Stegun §6.1 and the NIST Digital Library of Mathematical Functions §5.11. With those two correction terms the series and the direct sum agree to within about 8 × 10⁻¹⁶ in relative terms everywhere from n = 90 to 170, which is at the limit of what a double-precision number can resolve, and the digit count matches the true length of n! for every n from 0 to 170.
Where does Stirling's formula come from? ln(n!) is a sum of logarithms, and that sum is close to the area under the curve ln x from 1 to n, which integrates to n ln n − n + 1. The ½ ln(2πn) term and the small corrections account for the gap between the staircase of the sum and the smooth curve. Exponentiating the first three terms gives the familiar n! ≈ √(2πn) · (n/e)ⁿ. On its own that short form runs low by roughly 1/(12n) as a fraction: about 1.65% at n = 5, 0.83% at n = 10 and 0.083% at n = 100. The series is asymptotic rather than convergent, so adding ever more terms does not help indefinitely at a fixed n, but two corrections are plenty in the range used here.
A worked example.
Take n = 5, the default. Build the product one factor at a time: 1 × 2 = 2, then 2 × 3 = 6, then 6 × 4 = 24, then 24 × 5 = 120. So 5! = 120, and five books can stand on a shelf in 120 different orders — 24 of them with any particular book on the left, because once that book is fixed the other four can be arranged in 4! = 24 ways. For the digit count, log₁₀ 120 ≈ 2.0792; the whole-number part is 2, and adding one gives 3 digits, which is plainly right for 120. For the natural log, ln 120 = 4.7875. The same value comes from adding ln 2 + ln 3 + ln 4 + ln 5 ≈ 0.6931 + 1.0986 + 1.3863 + 1.6094, because the logarithm of a product is the sum of the logarithms of its factors. At n = 100 the three results become 9.333 × 10¹⁵⁷, 158 digits and 363.7394. The 158 follows from the logarithm: 363.7394 ÷ ln 10 (2.302585) ≈ 157.97, whose whole-number part is 157, plus one. That is the method to use whenever the factorial itself is too long to count by eye.
Frequently asked questions.
Why is 0! defined as 1 rather than 0?
Why can't I enter a number larger than 170?
How many zeros are at the end of 100!?
My program prints a wrong or negative number for 13! or 21!. What went wrong?
Does a fraction like (1/2)! have a value?
How do factorials give the number of poker hands or podium finishes?
Is n!! the factorial of a factorial?
How close is Stirling's approximation, and where is it used?
When is ln(n!) more useful than n! itself?
Do factorials have anything to do with prime numbers?
References& sources.
- [1]Knuth, Donald E. The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3rd edition. Addison-Wesley, 1997. §1.2.5 ('Permutations and Factorials') contains the canonical definition n! = 1·2·…·n with 0! = 1 by the empty-product convention, the standard recursion, and the integer-precision considerations for large n. §1.2.11.2 ('Stirling's Approximation') gives the asymptotic series with Bernoulli-number coefficients used by this calculator.
- [2]Hardy, G. H. and Wright, E. M. An Introduction to the Theory of Numbers, 6th edition, revised by Heath-Brown and Silverman. Oxford University Press, 2008. §1.2 (and the surrounding sections of Chapter I) establishes the factorial convention 0! = 1, the recursion n! = n·(n−1)!, the binomial coefficient identities derived from it, and the elementary number-theoretic properties (Wilson's theorem, divisibility of factorials by primes).
- [3]NIST Digital Library of Mathematical Functions, Chapter 5 ('Gamma Function'), §5.2 (Definition) and §5.11 (Asymptotic Expansions). Authoritative reference definitions of Γ(z), the integer relation Γ(n+1) = n!, the Stirling asymptotic series ln(n!) ~ n·ln(n) − n + ½·ln(2πn) + 1/(12n) − 1/(360n³) + …, and the analytic continuation to the complex plane.
- [4]Abramowitz, M. and Stegun, I. A. (eds.). Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables. National Bureau of Standards Applied Mathematics Series 55, 10th printing, 1972. §6.1 ('Gamma Function') contains the classical Stirling asymptotic series 6.1.41, half-integer factorial values, and the recursion identities — the canonical numerical-analysis reference for factorials and the gamma function.
- [5]Stirling, James. Methodus Differentialis: sive Tractatus de Summatione et Interpolatione Serierum Infinitarum. London, 1730. The original publication of Stirling's approximation in its asymptotic series form. Modern English translation and commentary in Tweddle, I. James Stirling's Methodus Differentialis, Springer, 2003.
- [6]Graham, R. L., Knuth, D. E., and Patashnik, O. Concrete Mathematics: A Foundation for Computer Science, 2nd edition. Addison-Wesley, 1994. Chapter 5 ('Binomial Coefficients') and §6.1 ('Stirling Numbers') give the modern computer-science treatment of factorial identities, hypergeometric-series perspectives, and the snake-oil method for proving factorial-ratio identities.
How this page was produced
- Published by
- Quanta Calculator
- Overseen by
- Daniel, Quanta Calculator Researcher
- Primary sources
- 6 cited below
- Method
- n! = n × (n−1) × (n−2) × ⋯ × 1
- Published
- Last verified
Built with AI assistance and verified by automated tests against the cited sources — every worked example on this page is computed by the same code that runs the calculator. How we build and check calculators.
In this category
Embed
Quanta Pro
Paid features are coming later.
- All 1560 calculators remain free
- No billing is enabled