Open In App

Find the last digit when factorial of A divides factorial of B

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 none 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));
 
        return variable % 10;
    }
}
 
// driver function
int main()
{
    cout << computeLastDigit(2632, 2634);
    return 0;
}


C




// CPP program to find last digit of a number
// obtained by dividing factorial of a number
// with factorial of another number.
#include <stdio.h>
 
// 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)
        return 1;
 
  // If difference (B - A) >= 5, answer = 0
    else if ((B - A) >= 5)
        return 0;
 
    else {
       
         // If none 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));
 
        return variable % 10;
    }
}
 
// driver function
int main()
{
    long long int a=2632;
    long long int b=2634;
    int ans=computeLastDigit(a,b);
    printf("%d",ans);
    return 0;
}
 
// This code is contributed by allwink45.


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 none 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 none 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 none 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.


Javascript




<script>
// Javascript 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)
{
 
    let 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 none 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 (let i = A + 1; i <= B; i++)
            variable = (variable * (i % 10)) % 10;
 
        return variable % 10;
    }
}
 
// driver function
 
    document.write(computeLastDigit(2632, 2634));
 
// This code is contributed by Surbhi Tyagi
 
</script>


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 none 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

Time Complexity: O(1).

Auxiliary Space: O( 1 ), since no extra space has been taken.

 

Using Recursion :

Approach:

This approach calculates the factorial of A using recursion and divides the factorial of B with it to get the last digit.

Define a function factorial(n) that takes an integer n and calculates its factorial using recursion.
Define another function last_digit(A, B) that takes two integers A and B.
Calculate the factorials of A and B using the factorial() function defined earlier.
Divide the factorial of B by the factorial of A to get the quotient.
Return the last digit of the quotient by using the modulus operator % with the divisor as 10.

C++




#include <iostream>
 
// Function to calculate the last digit of B! / A! without calculating factorials
int lastDigit(int A, int B) {
    int result = 1;
 
    // Calculate the last digit of each number from A+1 to B
    for (int i = A + 1; i <= B; ++i) {
        result *= (i % 10); // Keep only the last digit
        result %= 10;       // Ensure result remains a single digit
    }
 
    return result;
}
 
int main() {
    // Example usage
    std::cout << lastDigit(2, 4) << std::endl;        // Output: 2
    std::cout << lastDigit(107, 109) << std::endl;  // Output: 2
 
    // This Code is Contributed by Shivam Tiwari
    return 0;
}


Java




// Java Code for the above approach
 
public class GFG {
 
    // Function to calculate the last digit of B! / A!
    // without calculating factorials
    public static int lastDigit(int A, int B)
    {
        int result = 1;
 
        // Calculate the last digit of each number from A+1
        // to B
        for (int i = A + 1; i <= B; ++i) {
            result *= (i % 10); // Keep only the last digit
            result %= 10; // Ensure result remains a single
                          // digit
        }
 
        return result;
    }
 
    public static void main(String[] args)
    {
        // Example usage
        System.out.println(lastDigit(2, 4)); // Output: 2
        System.out.println(
            lastDigit(107, 109)); // Output: 2
    }
}
 
// This code is contributed by Susobhan Akhuli


Python3




def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n-1)
 
def last_digit(A, B):
    fact_A = factorial(A)
    fact_B = factorial(B)
    return fact_B // fact_A % 10
 
# Example usage
print(last_digit(2, 4)) # Output: 2
print(last_digit(107, 109)) # Output: 2


C#




using System;
 
class Program
{
    // Function to calculate the last digit of B! / A! without calculating factorials
    static int LastDigit(int A, int B)
    {
        int result = 1;
 
        // Calculate the last digit of each number from A+1 to B
        for (int i = A + 1; i <= B; ++i)
        {
            result *= (i % 10); // Keep only the last digit
            result %= 10;       // Ensure result remains a single digit
        }
 
        return result;
    }
 
    static void Main()
    {
        // Example usage
        Console.WriteLine(LastDigit(2, 4));         // Output: 2
        Console.WriteLine(LastDigit(107, 109));    // Output: 2
 
        //This Code is Contributed by Shivam Tiwari
    }
}


Javascript




function factorial(n) {
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}
 
function last_digit(A, B) {
  var fact_A = factorial(A);
  var fact_B = factorial(B);
  return Math.floor(fact_B / fact_A) % 10;
}
 
// Example usage
console.log(last_digit(2, 4)); // Output: 2
console.log(last_digit(107, 109)); // Output: 2


Output

2
2






Time complexity: O(A+B)
Space complexity: O(A)



Last Updated : 19 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads