We are given two numbers A and B such that B >= A. We need to compute the last digit of this resulting F such that F = B!/A! where 1 = A, B <= 10^18 (A and B are very large).
Examples:
Input : A = 2, B = 4 Output : 2 Explanation : A! = 2 and B! = 24. F = 24/2 = 12 --> last digit = 2 Input : 107 109 Output : 2
As we know, factorial function grows on an exponential rate. Even the largest data type
cannot hold factorial of numbers like 100. To compute factorial of moderately large numbers, refer this.
Here the given constraints are very large. Thus, calculating the two factorials and later
dividing them and computing the last digit is practically an impossible task.
Thus we have to find an alternate approach to break down our problem. It is known that the last digit of factorial always belongs to the set {0, 1, 2, 4, 6}
The approach is as follows: –
1) We evaluate the difference between B and A
2) If the (B – A) >= 5, then the answer is always 0
3) If the difference (B – A) < 5, then we iterate from (A+1) to B, multiply and store them. multiplication_answer % 10 shall be our answer.
C++
// CPP program to find last digit of a number // obtained by dividing factorial of a number // with factorial of another number. #include <iostream> using namespace std; // Function which computes the last digit // of resultant of B!/A! int computeLastDigit(long long int A, long long int B) { int variable = 1; if (A == B) // If A = B, B! = A! and B!/A! = 1 return 1; // If difference (B - A) >= 5, answer = 0 else if ((B - A) >= 5) return 0; else { // If non of the conditions are true, we // iterate from A+1 to B and multiply them. // We are only concerned for the last digit, // thus we take modulus of 10 for (long long int i = A + 1; i <= B; i++) variable = (variable * (i % 10)) % 10; return variable % 10; } } // driver function int main() { cout << computeLastDigit(2632, 2634); return 0; } |
Java
// Java program to find last digit of a number // obtained by dividing factorial of a number // with factorial of another number. import java.io.*; class GFG { // Function which computes the last digit // of resultant of B!/A! static int computeLastDigit(long A, long B) { int variable = 1; if (A == B) // If A = B, B! = A! and B!/A! = 1 return 1; // If difference (B - A) >= 5, answer = 0 else if ((B - A) >= 5) return 0; else { // If non of the conditions are true, we // iterate from A+1 to B and multiply them. // We are only concerned for the last digit, // thus we take modulus of 10 for (long i = A + 1; i <= B; i++) variable = (int)(variable * (i % 10)) % 10; return variable % 10; } } // driver function public static void main(String[] args) { System.out.println(computeLastDigit(2632, 2634)); } } // This article is contributed by Prerna Saini |
Python3
# Python program to find # last digit of a number # obtained by dividing # factorial of a number # with factorial of another number. # Function which computes # the last digit # of resultant of B!/A! def computeLastDigit(A,B): variable = 1 if (A == B): # If A = B, B! = A! and B!/A! = 1 return 1 # If difference (B - A) >= 5, answer = 0 elif ((B - A) >= 5): return 0 else: # If non of the conditions # are true, we # iterate from A+1 to B # and multiply them. # We are only concerned # for the last digit, # thus we take modulus of 10 for i in range(A + 1, B + 1): variable = (variable * (i % 10)) % 10 return variable % 10 # driver function print(computeLastDigit(2632, 2634)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find last digit of // a number obtained by dividing // factorial of a number with // factorial of another number. using System; class GFG { // Function which computes the last // digit of resultant of B!/A! static int computeLastDigit(long A, long B) { int variable = 1; // If A = B, B! = A! // and B!/A! = 1 if (A == B) return 1; // If difference (B - A) >= 5, // answer = 0 else if ((B - A) >= 5) return 0; else { // If non of the conditions are true, we // iterate from A+1 to B and multiply them. // We are only concerned for the last digit, // thus we take modulus of 10 for (long i = A + 1; i <= B; i++) variable = (int)(variable * (i % 10)) % 10; return variable % 10; } } // Driver Code public static void Main() { Console.WriteLine(computeLastDigit(2632, 2634)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find last digit of a number // obtained by dividing factorial of a number // with factorial of another number. // Function which computes the last // digit of resultant of B!/A! function computeLastDigit($A, $B) { $variable = 1; // If A = B, B! = A! // and B!/A! = 1 if ($A == $B) return 1; // If difference (B - A) >= 5, // answer = 0 else if (($B - $A) >= 5) return 0; else { // If non of the conditions // are true, we iterate from // A+1 to B and multiply them. // We are only concerned for // the last digit, thus we // take modulus of 10 for ($i = $A + 1; $i <= $B; $i++) $variable = ($variable * ($i % 10)) % 10; return $variable % 10; } } // Driver Code echo computeLastDigit(2632, 2634); // This code is contributed by ajit ?> |
Output:
2
This article is contributed by Shivani Mittal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Recommended Posts:
- Last digit of a number raised to last digit of N factorial
- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Find last digit in factorial
- Find maximum power of a number that divides a factorial
- Find the remainder when First digit of a number is divided by its Last digit
- Last non-zero digit of a factorial
- 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
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count of N-digit numbers having digit XOR as single digit
- Find the last two digits of Factorial of a given Number
- Program to find last digit of n'th Fibonnaci Number
- Find Last Digit of a^b for Large Numbers
- Find the last digit of given series
- Find last five digits of a given five digit number raised to power five
- Program to find the last digit of X in base Y
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)

