How this calculation works
The last digit of a sufficiently large factorial is zero, but the last nonzero digit contains different information. Remove every trailing decimal zero, then inspect the remaining units digit. Constructing n! is unnecessary: groups of five account for factors of five, each of which cancels a factor of two to make a trailing zero. A short recurrence tracks the remaining units digit through successively smaller arguments.
D(n) = D(⌊n/5⌋) D(n mod 5) 2^⌊n/5⌋ mod 10 for n ≥ 5; D(0..4) = 1,1,2,6,4.A worked example
10! = 3,628,800. Removing its two trailing zeros leaves 36,288, so the answer is 8. The recurrence gives D(10) = D(2) × D(0) × 2² modulo 10 = 2 × 1 × 4 modulo 10 = 8. The answer is a digit, while the separate trailing-zero count for this input is two.
Good to know
Only the final nonzero base-ten digit is calculated. It does not return the last several digits or support other number bases. Inputs are whole numbers from zero through one trillion. This specialized recurrence cannot be applied unchanged to an arbitrary product or a factorial quotient.
Reference: OEIS A008904 — last nonzero digit of n factorial (opens in a new tab) ↗
Removing zeros does not mean dropping every factor of ten
A trailing zero comes from a factor pair 2 × 5. Some fives arrive inside multiples of 25 and contribute more than one pair. Simply skipping terms divisible by ten would therefore lose information: the factors 2 and 5 can come from different factorial terms. The recurrence handles these cancellations at the prime-factor level and retains the unmatched factors of two.
The first five values are easy to establish directly: 0! and 1! are both 1, 2! is 2, 3! is 6, and 4! is 24 with final nonzero digit 4. These are the base cases. Every larger argument reduces to its quotient on division by five and one of those base cases, together with a power-of-two correction.
Read the trace from the smaller factorial upward
For n = 25, the recursive arguments are 25, 5 and 1. Starting from D(1) = 1, obtain D(5) = 1 × 1 × 2 = 2. Then D(25) = 2 × 1 × 2⁵ modulo 10 = 4. Each trace row records a complete recurrence evaluation, rather than merely the next digit of a large factorial. This makes a large-input answer checkable with small intermediate values.
The number of recursive levels grows logarithmically in n. Even one trillion reduces after only a short chain of divisions by five, while repeated squaring evaluates powers modulo ten without building the full power. The result remains exact within the stated range. Use the existing factorial trailing-zeros calculator when the question is how many zeros are removed, and factorial prime valuation when the question is the exponent of a particular prime.
A few questions, answered
What is the answer for zero factorial?
0! is defined as 1, so its last nonzero digit is 1. Zero is a valid input.
Can the returned digit be odd?
For n at least 2, the final nonzero digit is even. After all factors of five have been paired off, sufficient factors of two remain.