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 firstname.lastname@example.org. 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.
- Find the last digit when factorial of A divides factorial of B
- Last digit of a number raised to last digit of N factorial
- Find last digit in factorial
- First digit in factorial of a number
- Maximum number with same digit factorial product
- Perform n steps to convert every digit of a number in the format [count][digit]
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find the remainder when First digit of a number is divided by its Last digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Largest number less than N with digit sum greater than the digit sum of N
- Count 'd' digit positive integers with 0 as a digit
- Check if frequency of each digit is less than the digit
- Count n digit numbers not having a particular digit
- Double factorial
- LCM of factorial and its neighbors
- Find the next Factorial greater than N
- Check if N-factorial is divisible by X^Y
- C Program for factorial of a number