Factorial calculator
The number of ways to shuffle a deck of cards is 52!, roughly 8 × 10⁶⁷. That is more than the estimated number of atoms in our galaxy. Every properly shuffled deck in history has almost certainly been in an order never seen before and never to be seen again, which is the most vivid demonstration of factorial growth anyone has come up with.
A factorial is every whole number up to n multiplied together. 10! is 3,628,800. Growth is extraordinarily fast — 20! is 2,432,902,008,176,640,000, and 52! is about 8 × 10⁶⁷.
How to calculate a factorial
Zero factorial is defined as one, which looks arbitrary and is not. It falls out of the definition n! = n × (n−1)!: applying it at n = 1 gives 1! = 1 × 0!, so 0! must be 1. It is also what makes the combination formula work. C(n,n) should be 1, meaning there is exactly one way to choose everything, and that only comes out right if 0! = 1. Past about 20 the exact digits stop being the interesting part and the size does. Stirling’s approximation, n! ≈ √(2πn) × (n∕e)ⁿ, gives it directly: at n = 10 it returns 3,598,696 against a true 3,628,800, low by 0.83 per cent, and the relative error keeps shrinking as n grows. That is why serious code almost never holds n! itself: it holds log(n!), which is still a modest number long after n! has stopped fitting into anything.
Questions
3,628,800.
Because n! = n × (n−1)! requires it, and because there is exactly one way to arrange nothing.
About 8 × 10⁶⁷: more than the estimated number of atoms in the Milky Way.
Twelve. Every trailing zero needs a factor of five paired with a two, and twos are far more plentiful, so the fives do all the limiting: ⌊52∕5⌋ + ⌊52∕25⌋ = 10 + 2.
Stirling’s formula, √(2πn) × (n∕e)ⁿ. It is 0.83 per cent low at n = 10 and better above that.
Not directly. The gamma function extends factorials to non-integers, where Γ(n+1) = n!.
Because 171! overflows a double-precision float: 170! is about 7.3 × 10³⁰⁶ and the next step is infinity. Beyond that you need arbitrary-precision arithmetic.