Last Nonzero Factorial Digit
Time limit0.3sMemory limit128 MB
Compute the last nonzero digit of N! for N up to 20000 without directly computing the huge factorial.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Simulation
- Solved
- No attempts yet
Problem
Given a positive integer N, write N! in decimal notation and find the rightmost digit that is not 0.
For example, 4! = 24, so the answer is 4. Also, 5! = 120, so the answer is 2.
Input
The first line contains a positive integer N. N is at most 20,000.
Output
Output one digit: the last nonzero digit of N!.