Given a number n, find the last non-zero digit in n!.
Input : n = 5 Output : 2 5! = 5 * 4 * 3 * 2 * 1 = 120 Last non-zero digit in 120 is 2. Input : n = 33 Output : 4
A Simple Solution is to first find n!, then find last non-zero digit of n. This solution doesn’t work for even slightly large numbers due to arithmetic overflow.
A Better Solution is based on below recursive formula
Let D(n) be the last non-zero digit in n! If tens digit (or second last digit) of n is odd D(n) = 4 * D(floor(n/5)) * D(Unit digit of n) If tens digit (or second last digit) of n is even D(n) = 6 * D(floor(n/5)) * D(Unit digit of n)
Illustration of formula:
For the numbers less than 10 we can easily find the last non-zero digit by above simple solution, i.e., first computing n!, then finding last digit.
D(1) = 1, D(2) = 2, D(3) = 6, D(4) = 4, D(5) = 2,
D(6) = 2, D(7) = 4, D(8) = 2, D(9) = 8.
D(1) to D(9) are assumed to be precomputed. Example 1: n = 27 [Second last digit is even]: D(27) = 6 * D(floor(27/5)) * D(7) = 6 * D(5) * D(7) = 6 * 2 * 4 = 48 Last non-zero digit is 8 Example 2: n = 33 [Second last digit is odd]: D(33) = 4 * D(floor(33/5)) * D(3) = 4 * D(6) * 6 = 4 * 2 * 6 = 48 Last non-zero digit is 8
How does above formula work?
The below explanation provides intuition behind the formula. Readers may refer Refer http://math.stackexchange.com/questions/130352/last-non-zero-digit-of-a-factorial for complete proof.
14! = 14 * 13 * 12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 Since we are asked about last non-zero digit, we remove all 5's and equal number of 2's from factors of 14!. We get following: 14! = 14 * 13 * 12 * 11 * 2 * 9 * 8 * 7 * 6 * 3 * 2 * 1 Now we can get last non-zero digit by multiplying last digits of above factors!
In n!, number of 2’s are always more than number of 5’s. To remove trailing 0’s, we remove 5’s and equal number of 2’s.
Let a = floor(n/5), b = n % 5. After removing equal number of 5’s and 2’s, we can reduce the problem from n! to 2a * a! * b!
D(n) = 2a * D(a) * D(b)
This article is contributed by Niteesh kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Last digit of a number raised to last digit of N factorial
- Find the last digit when factorial of A divides factorial of B
- Find last digit in factorial
- Find the last two digits of Factorial of a given Number
- First digit in factorial of a number
- Maximum number with same digit factorial product
- Program to find last digit of n'th Fibonnaci Number
- Find Last Digit of a^b for Large Numbers
- N digit numbers having difference between the first and last digits as K
- Perform n steps to convert every digit of a number in the format [count][digit]
- Count digits in a factorial | Set 2
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Count factorial numbers in a given range
- Count digits in a factorial | Set 1
- Count Divisors of Factorial
- Find the first natural number whose factorial is divisible by x
- Smallest number with at least n trailing zeroes in factorial
- Smallest number with at least n digits in factorial
- Double factorial