Find the last digit when factorial of A divides factorial of B
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++
#include <iostream>
using namespace std;
int computeLastDigit( long long int A, long long int B)
{
int variable = 1;
if (A == B)
return 1;
else if ((B - A) >= 5)
return 0;
else {
for ( long long int i = A + 1; i <= B; i++)
variable = (variable * (i % 10));
return variable % 10;
}
}
int main()
{
cout << computeLastDigit(2632, 2634);
return 0;
}
|
C
#include <stdio.h>
int computeLastDigit( long long int A, long long int B)
{
int variable = 1;
if (A == B)
return 1;
else if ((B - A) >= 5)
return 0;
else {
for ( long long int i = A + 1; i <= B; i++)
variable = (variable * (i % 10));
return variable % 10;
}
}
int main()
{
long long int a=2632;
long long int b=2634;
int ans=computeLastDigit(a,b);
printf ( "%d" ,ans);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int computeLastDigit( long A, long B)
{
int variable = 1 ;
if (A == B)
return 1 ;
else if ((B - A) >= 5 )
return 0 ;
else {
for ( long i = A + 1 ; i <= B; i++)
variable = ( int )(variable * (i % 10 )) % 10 ;
return variable % 10 ;
}
}
public static void main(String[] args)
{
System.out.println(computeLastDigit( 2632 , 2634 ));
}
}
|
Python3
def computeLastDigit(A,B):
variable = 1
if (A = = B):
return 1
elif ((B - A) > = 5 ):
return 0
else :
for i in range (A + 1 , B + 1 ):
variable = (variable * (i % 10 )) % 10
return variable % 10
print (computeLastDigit( 2632 , 2634 ))
|
C#
using System;
class GFG {
static int computeLastDigit( long A, long B)
{
int variable = 1;
if (A == B)
return 1;
else if ((B - A) >= 5)
return 0;
else {
for ( long i = A + 1; i <= B; i++)
variable = ( int )(variable *
(i % 10)) % 10;
return variable % 10;
}
}
public static void Main()
{
Console.WriteLine(computeLastDigit(2632, 2634));
}
}
|
Javascript
<script>
function computeLastDigit(A, B)
{
let variable = 1;
if (A == B)
return 1;
else if ((B - A) >= 5)
return 0;
else {
for (let i = A + 1; i <= B; i++)
variable = (variable * (i % 10)) % 10;
return variable % 10;
}
}
document.write(computeLastDigit(2632, 2634));
</script>
|
PHP
<?php
function computeLastDigit( $A , $B )
{
$variable = 1;
if ( $A == $B )
return 1;
else if (( $B - $A ) >= 5)
return 0;
else
{
for ( $i = $A + 1; $i <= $B ; $i ++)
$variable = ( $variable * ( $i % 10)) % 10;
return $variable % 10;
}
}
echo computeLastDigit(2632, 2634);
?>
|
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>
int lastDigit( int A, int B) {
int result = 1;
for ( int i = A + 1; i <= B; ++i) {
result *= (i % 10);
result %= 10;
}
return result;
}
int main() {
std::cout << lastDigit(2, 4) << std::endl;
std::cout << lastDigit(107, 109) << std::endl;
return 0;
}
|
Java
public class GFG {
public static int lastDigit( int A, int B)
{
int result = 1 ;
for ( int i = A + 1 ; i <= B; ++i) {
result *= (i % 10 );
result %= 10 ;
}
return result;
}
public static void main(String[] args)
{
System.out.println(lastDigit( 2 , 4 ));
System.out.println(
lastDigit( 107 , 109 ));
}
}
|
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
print (last_digit( 2 , 4 ))
print (last_digit( 107 , 109 ))
|
C#
using System;
class Program
{
static int LastDigit( int A, int B)
{
int result = 1;
for ( int i = A + 1; i <= B; ++i)
{
result *= (i % 10);
result %= 10;
}
return result;
}
static void Main()
{
Console.WriteLine(LastDigit(2, 4));
Console.WriteLine(LastDigit(107, 109));
}
}
|
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;
}
console.log(last_digit(2, 4));
console.log(last_digit(107, 109));
|
Time complexity: O(A+B)
Space complexity: O(A)
Last Updated :
19 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...