我们给了两个数字A和B,使得B> =A。我们需要计算该结果F的最后一位,以使F = B!/ A!。其中1 = A,B <= 10 ^ 18(A和B很大)。
例子:
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
众所周知,阶乘函数以指数速率增长。即使是最大的数据类型
不能容纳数字的阶乘,例如100。要计算中等大数的阶乘,请参考此内容。
这里给定的约束非常大。因此,计算两个阶乘
将它们相除并计算最后一位数字实际上是不可能的任务。
因此,我们必须找到替代方法来解决我们的问题。众所周知,阶乘的最后一位数字始终属于集合{0,1,2,4,4,6}
方法如下:–
1)我们评估B和A之间的差异
2)如果(B – A)> = 5,则答案始终为0
3)如果差(B – A)<5,则我们从(A + 1)迭代到B,将它们相乘并存储。我们必须使用multiplication_answer%10。
C++
// CPP program to find last digit of a number
// obtained by dividing factorial of a number
// with factorial of another number.
#include
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
= 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
?>
Javascript
输出:
2