A permutation is a specific ordered arrangement of a set of distinct objects. The total number of such arrangements for $n$ objects is given by the factorial function $n!$, one of the most fundamental constructs in combinatorics, probability theory, and algorithm design.
The practical challenge with factorials is their explosive growth. While $10! = 3{,}628{,}800$ fits comfortably in memory, $100!$ produces a 158-digit integer that overflows standard 64-bit floating-point arithmetic. This tool computes exact factorial values using arbitrary-precision integer arithmetic, alongside a suite of derived metrics — derangements, circular permutations, trailing zeros, and Stirling's approximation — that expose the deeper structure hidden within $n!$.
Required Project Parameters
- Total Items ($n$): The number of distinct objects to arrange. This value must be a non-negative integer. The calculator enforces a ceiling of $n = 2000$ to prevent browser memory exhaustion during BigInt iteration. Setting $n = 0$ returns the identity case $0! = 1$.
- Generation Speed (operations per second): A theoretical benchmark representing hypothetical hardware throughput, ranging from $10^6$ (one million) to $10^{18}$ (one quintillion) operations per second. This parameter is used exclusively to contextualize how long it would take to physically enumerate every permutation — not to compute the factorial itself.
The Algebra of Arrangement: Core Formulas and Derivations
The Factorial Recurrence
The factorial function is defined recursively as:
$$n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1$$
with the base case $0! = 1$ by convention. This convention is not arbitrary — it is required for the binomial coefficient identity $\binom{n}{0} = 1$ to hold and for the Gamma function relation $\Gamma(1) = 0! = 1$ to remain consistent.
For computational purposes, the calculator builds $n!$ iteratively using JavaScript's native BigInt type, multiplying a running product by each successive integer from $2$ to $n$. Standard IEEE 754 doubles lose integer precision beyond $2^{53}$, which corresponds to approximately $n = 170$. BigInt eliminates this ceiling entirely, delivering exact results for $n$ up to 2000 and beyond.
Derangements and the Subfactorial
A derangement is a permutation in which no element appears in its original position. The number of derangements of $n$ elements, denoted $!n$ or $D_n$, satisfies the recurrence:
$$D_n = (n - 1)(D_{n-1} + D_{n-2})$$
with initial conditions $D_0 = 1$ and $D_1 = 0$. The closed-form expression is:
$$D_n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}$$
which follows directly from the inclusion-exclusion principle applied to the $n$ fixed-point conditions.
The derangement probability — the chance that a random permutation is a derangement — converges with remarkable speed:
$$\frac{D_n}{n!} = \sum_{k=0}^{n} \frac{(-1)^k}{k!} \xrightarrow{n \to \infty} \frac{1}{e} \approx 0.367879$$
By $n = 7$, this ratio already agrees with $1/e$ to six decimal places. For $n > 170$, where the floating-point ratio $D_n / n!$ cannot be computed directly from BigInt division without precision loss, the calculator hardcodes the limiting value of $1/e \approx 36.79\%$.
Trailing Zeros via Legendre's Formula
Every trailing zero in $n!$ arises from a factor of $10 = 2 \times 5$. Since even numbers contribute an excess of factors of 2, the count of trailing zeros is determined entirely by the multiplicity of the prime 5 in the factorization of $n!$.
Legendre's formula for the $p$-adic valuation gives:
$$\nu_5(n!) = \sum_{i=1}^{\infty} \left\lfloor \frac{n}{5^i} \right\rfloor$$
The sum terminates when $5^i > n$. For example, $100!$ has $\lfloor 100/5 \rfloor + \lfloor 100/25 \rfloor = 20 + 4 = 24$ trailing zeros. This is far more efficient than converting the entire BigInt result to a string and counting terminal digits.
Stirling's Approximation in Logarithmic Space
For large $n$, the direct computation of Stirling's classical formula $n! \approx \sqrt{2\pi n}\left(\frac{n}{e}\right)^n$ overflows any floating-point representation. The calculator sidesteps this by working in $\log_{10}$ space:
$$\log_{10}(n!) \approx \frac{n \ln(n) - n + 0.5 \ln(2\pi n)}{\ln(10)}$$
The integer part of this result gives the exponent, and $10$ raised to the fractional part gives the mantissa, yielding a clean scientific notation output (e.g., $5.713 \times 10^{2567}$). This technique is standard in statistical mechanics, where physicists compute $\ln(N!)$ for particle counts on the order of Avogadro's number.
Circular Permutations
When objects are arranged in a circle, rotations of the same linear arrangement are considered identical. The number of distinct circular permutations of $n$ objects is:
$$(n - 1)!$$
This reduction by a factor of $n$ follows from fixing one element's position to eliminate rotational symmetry.
Pairwise Combinations
The calculator also reports $C(n, 2)$, the number of unique unordered pairs:
$$\binom{n}{2} = \frac{n(n - 1)}{2}$$
This serves as a quick reference for problems involving handshakes, round-robin matchups, or edge counts in complete graphs.
Factorial Growth: A Quantitative Reference
The following table illustrates how rapidly $n!$ scales, along with derived metrics at key values:
| $n$ | $n!$ | Digits | Trailing Zeros | Derangements $D_n$ | Circular $(n-1)!$ |
|---|---|---|---|---|---|
| 5 | 120 | 3 | 1 | 44 | 24 |
| 10 | 3,628,800 | 7 | 2 | 1,334,961 | 362,880 |
| 15 | 1,307,674,368,000 | 13 | 3 | 481,066,515,734 | 87,178,291,200 |
| 20 | 2.432 $\times 10^{18}$ | 19 | 4 | 8.946 $\times 10^{17}$ | 1.216 $\times 10^{17}$ |
| 52 | 8.066 $\times 10^{67}$ | 68 | 12 | 2.967 $\times 10^{67}$ | 1.551 $\times 10^{66}$ |
| 100 | 9.333 $\times 10^{157}$ | 158 | 24 | 3.433 $\times 10^{157}$ | 9.333 $\times 10^{155}$ |
| 170 | 7.257 $\times 10^{306}$ | 307 | 41 | 2.670 $\times 10^{306}$ | 4.269 $\times 10^{304}$ |
| 1000 | 4.024 $\times 10^{2567}$ | 2568 | 249 | 1.480 $\times 10^{2567}$ | 4.024 $\times 10^{2564}$ |
The $n = 170$ row marks the critical IEEE 754 precision boundary — the largest factorial representable as a standard JavaScript Number. Beyond this point, only BigInt arithmetic preserves the exact integer value.
The $n = 52$ row corresponds to a standard deck of playing cards. The number of possible shuffles, $52! \approx 8.07 \times 10^{67}$, is so astronomically large that no two properly randomized shuffles in the history of card games have ever produced the same ordering.
Computational Time Estimates at Scale
The table below contextualizes $O(n!)$ time complexity by projecting how long it would take to enumerate every permutation at different theoretical processing speeds:
| $n$ | $n!$ | At $10^9$ ops/sec | At $10^{12}$ ops/sec | At $10^{15}$ ops/sec |
|---|---|---|---|---|
| 10 | 3.63 $\times 10^6$ | 3.63 ms | 3.63 $\mu$s | 3.63 ns |
| 15 | 1.31 $\times 10^{12}$ | 21.8 min | 1.31 s | 1.31 ms |
| 20 | 2.43 $\times 10^{18}$ | 77.1 years | 28.1 days | 40.5 min |
| 25 | 1.55 $\times 10^{25}$ | 491 million years | 491,000 years | 491 years |
| 50 | 3.04 $\times 10^{64}$ | $9.64 \times 10^{47}$ years | $9.64 \times 10^{44}$ years | $9.64 \times 10^{41}$ years |
The transition from $n = 15$ to $n = 20$ is where brute-force enumeration crosses the threshold from feasible to physically impossible. Even at one trillion operations per second — roughly the throughput of a modern GPU cluster — exhausting $20!$ permutations requires nearly a month. At $n = 25$, no hardware that could ever exist in this universe would complete the enumeration before the heat death of the stars.
This is the fundamental reason why cryptographic key spaces, password entropy calculations, and NP-hard optimization problems rely on factorial and exponential growth as their security guarantee.
From Theory to Practice: Interpreting Factorial-Scale Results
The $O(n!)$ Wall in Algorithm Design
Calculating the value of $n!$ is trivial — a simple loop completes in microseconds even for $n = 2000$. But generating all $n!$ permutations is an entirely different computational class. This distinction is central to understanding why algorithms with factorial time complexity, such as naive solutions to the Travelling Salesman Problem (TSP), are classified as intractable.
The "Time to Generate" output makes this concrete. A researcher investigating a 15-city TSP can enumerate all $1.31 \times 10^{12}$ routes in about 22 minutes on a modern processor. Add just five more cities and the same task requires 77 years. This exponential cliff is why heuristic and approximation algorithms — simulated annealing, genetic algorithms, branch-and-bound — dominate practical optimization.
The Secret Santa Phenomenon and Derangement Convergence
The derangement probability metric reveals a counterintuitive result from combinatorics. Consider a group of colleagues randomly drawing names for a gift exchange. The probability that nobody draws their own name is the derangement ratio $D_n / n!$.
For a group of 5, this probability is $44/120 = 36.67\%$. For a group of 10, it is $1{,}334{,}961 / 3{,}628{,}800 \approx 36.79\%$. For a group of 1,000, it is indistinguishable from $1/e \approx 36.79\%$.
The convergence is extraordinarily fast — the ratio stabilizes to five decimal places by $n = 7$. This means the size of the group is essentially irrelevant. Whether there are 10 participants or 10,000, the odds of a "perfect" draw (no self-assignments) remain locked at roughly 36.8%. This result, derived from the alternating series $\sum (-1)^k / k!$, is one of the earliest appearances of $e$ in discrete combinatorics.
Prime Factorization and the Anatomy of Trailing Zeros
The trailing zeros output provides a window into the prime decomposition of $n!$. Every trailing zero corresponds to one factor of 10, which requires one factor of 2 and one factor of 5. Because even numbers are far more abundant than multiples of 5, the count of fives is always the bottleneck.
Legendre's formula captures this by summing $\lfloor n/5 \rfloor + \lfloor n/25 \rfloor + \lfloor n/125 \rfloor + \cdots$, counting not just direct multiples of 5 but also those of $25 = 5^2$, $125 = 5^3$, and so on. The number 25 contributes two factors of 5, the number 125 contributes three, and so forth. This layered counting is a direct application of the $p$-adic valuation from number theory.
Stirling's Approximation: Bridging Discrete and Continuous Mathematics
Stirling's approximation is not merely a computational shortcut — it is a critical bridge between combinatorics and continuous physics. In statistical mechanics, the entropy of a system of $N$ particles is proportional to $\ln(N!)$, where $N$ approaches Avogadro's number ($6.022 \times 10^{23}$). Computing exact factorials at that scale is absurd; Stirling's formula provides the asymptotic accuracy needed for thermodynamic calculations.
The relative error of Stirling's approximation decreases as $O(1/n)$. At $n = 10$, it overestimates by about 0.83%. At $n = 100$, the error drops below 0.083%. By $n = 1000$, the approximation agrees with the exact value to better than one part in 12,000. The logarithmic computation method used here — splitting $\log_{10}(n!)$ into mantissa and exponent — ensures that even $n = 2000$ produces a clean scientific notation result without overflow.
Frequently Asked Questions
The exact integer value of $n!$ is always computed internally using BigInt arithmetic, regardless of $n$. For display purposes, however, an integer with thousands of digits becomes impractical to render or interpret. Beyond approximately $n = 25$, the result is presented in scientific notation (e.g., $1.551 \times 10^{25}$) alongside the total digit count.
The full exact value remains available — the digit count tells the user precisely how large the result is. For context, $1000!$ contains 2,568 digits. Printing it in 12-point font would fill roughly three full pages of text. The scientific notation format preserves the critical information (magnitude and leading digits) while keeping the output readable.
Derangements are computed using the iterative recurrence $D_n = (n - 1)(D_{n-1} + D_{n-2})$ with BigInt arithmetic, which runs in $O(n)$ time and avoids the combinatorial explosion of the inclusion-exclusion sum. This is efficient even for $n = 2000$.
The potential bottleneck arises in computing the ratio $D_n / n!$, which requires dividing two BigInts that may have thousands of digits. Since JavaScript BigInt division returns an integer quotient, extracting a meaningful decimal probability from two 2,000-digit numbers is non-trivial. The calculator resolves this by computing the ratio directly only up to $n = 170$ (where standard floating-point division remains precise) and substituting the known limit $1/e \approx 0.367879$ for all larger values. The mathematical justification is that $D_n / n!$ converges to $1/e$ with an error bounded by $1/(n+1)!$, making the substitution exact to any practical precision well before $n = 170$.
The value $n = 170$ is the largest integer for which $n!$ fits within the IEEE 754 double-precision floating-point range, which caps at approximately $1.797 \times 10^{308}$. Since $170! \approx 7.257 \times 10^{306}$ and $171! \approx 1.241 \times 10^{309}$, any computation relying on standard Number types silently returns Infinity at $n = 171$.
This boundary affects every programming language and spreadsheet application that uses 64-bit floats: Excel, Python's math.factorial (which internally switches to arbitrary-precision integers), Google Sheets, and most scientific calculators. The permutations calculator bypasses this limit entirely through BigInt iteration, but the threshold remains relevant for the derangement ratio calculation and for Stirling's approximation, both of which use floating-point logarithms.
Precision at Scale: The Case for Automated Factorial Analysis
Manual factorial computation is error-prone beyond $n = 10$ and practically impossible beyond $n = 20$. The derived metrics — derangements, trailing zeros, circular permutations — compound the difficulty by requiring additional number-theoretic techniques that are easy to misapply.
Automated computation eliminates these risks while simultaneously exposing the structural relationships within $n!$ that manual calculation obscures. The transition from exact BigInt arithmetic to logarithmic Stirling approximation, the Legendre formula's efficient zero-counting, and the derangement recurrence's $O(n)$ performance all represent algorithmic choices that balance precision against computational feasibility. For researchers, educators, and engineers working at the intersection of combinatorics and computation, these are not merely convenient outputs — they are the analytical tools that make factorial-scale reasoning tractable.