Given two numbers A and B. The task is to compute the last digit of the resulting F, where F= B! / A! .
Example 1:
Input: A = 2, B = 4
Output: 2
Explaination: The value of F = 12. So the 
last digit is 2.
Example 2:
Input: A = 3, B = 8
Output: 0
Explaination: F is 6720. So the last value 
is 0.
Your Task:
You do not need to read the input or print anything. Your task is to complete the function lastDigit() which takes A and B and returns the last value of F.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints: 
1 ≤ A ≤ B ≤ 10^{18}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def lastDigit(self, A, B):

        # code here

Solutions:
class Solution:

	def lastDigit(self, A, B):
		ans = 1
		for i in range(A + 1, min(B + 1, A + 11)):
			ans = ans * i % 10
		return ans


import math as m

class Solution:

	def lastDigit(self, A, B):
		product = 1
		for i in range(A + 1, B + 1):
			product = product * (i % 10) % 10
			if product == 0:
				return 0
		return product


class Solution:

	def lastDigit(self, A, B):
		n = B - A
		if n >= 5:
			return 0
		else:
			temp = 1
			for i in range(n):
				temp *= B - i
			return temp % 10


class Solution:

	def lastDigit(self, a, b):
		k = b - a
		i = 1
		t = a + 1
		r = 1
		while k > 0 and i < 6:
			k -= 1
			i += 1
			r *= t
			t += 1
		return r % 10


from math import factorial as fac

class Solution:

	def lastDigit(self, A, B):
		if B == A:
			return 1
		if B - A == 1:
			return B % 10
		if B - A == 2:
			a = B * (B - 1)
			return a % 10
		if B - A == 3:
			a = B * (B - 1) * (B - 2)
			return a % 10
		if B - A == 4:
			a = B * (B - 1) * (B - 2) * (B - 3)
			return a % 10
		else:
			return 0


class Solution:

	def lastDigit(self, A, B):
		prod = 1
		if B - A >= 10:
			return 0
		for i in range(B, A, -1):
			prod *= i
		return str(prod)[-1]


class Solution:

	def lastDigit(self, A, B):
		v = 1
		if A == B:
			return 1
		elif B - A >= 5:
			return 0
		else:
			for i in range(A + 1, B + 1):
				v = v * (i % 10) % 10
		return v % 10


class Solution:

	def lastDigit(self, A, B):
		a = 1
		for i in range(B - A):
			a *= B
			B -= 1
		return str(a)[-1]


class Solution:

	def lastDigit(self, A, B):
		return str(self.factorial(A, B))[-1]

	def factorial(self, A, B):
		if B > A:
			return self.factorial(A, B - 1) * B
		else:
			return 1


class Solution:

	def lastDigit(self, A, B):
		F = 1
		for i in range(A + 1, B + 1):
			F *= i
		return F % 10


class Solution:

	def lastDigit(self, A, B):
		s = 1
		while B > A:
			s = s * B
			s = s % 10
			B -= 1
		return s


class Solution:

	def lastDigit(self, A, B):
		prod = 1
		while B > A:
			prod *= B
			prod = prod % 10
			B -= 1
		return int(prod)


import math

class Solution:

	def lastDigit(self, A, B):
		a = 1
		if A == B:
			return 1
		elif B - A >= 5:
			return 0
		else:
			for i in range(A + 1, B + 1):
				a = a * i % 10 % 10
			return a % 10


class Solution:

	def lastDigit(self, A, B):
		x = 1
		while B > A:
			x = x * B
			B = B - 1
		return x % 10


import math

class Solution:

	def lastDigit(self, A, B):
		ans = 1
		while B > A:
			ans = ans * B
			ans = ans % 10
			B -= 1
		return int(ans)


class Solution:

	def lastDigit(self, A, B):

		def factorial(n, A):
			fact = 1
			for i in range(A + 1, n + 1):
				fact *= i
			return fact
		a = factorial(B, A)
		a = str(a)
		return a[len(a) - 1]


class Solution:

	def lastDigit(self, A, B):
		res = 1
		if A > B:
			return 0
		elif B == A:
			return 1
		elif B > A:
			for i in range(A + 1, B + 1):
				res = res * i
		if res < 10:
			return res
		return res % 10