Given a number n, find the last non-zero digit in n!.
Examples:
Input  : n = 5
Output : 2
5! = 5 * 4 * 3 * 2 * 1 = 120
Last non-zero digit in 120 is 2.
Input  : n = 33
Output : 8
Your Task:  
You don't need to read input or print anything. Your task is to complete the function lastNon0Digit() which takes the integer N as input parameters and returns the last non-zero digit in n!.
Expected Time Complexity: O(N^2)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 100

Difficulty: EASY
Tags: Mathematics

Complete search
Skill types: Complete search
Expected Time Complexity: O(N^2)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3


class Solution:
    def lastNon0Digit (self, N):
        pass
        # code here
    


Solutions:
class Solution:

	def lastNon0Digit(self, N):
		self.ans = 1

		def recursion(N):
			if N == 1:
				return 1
			self.ans *= N
			recursion(N - 1)
		recursion(N)
		str_ans = str(self.ans)
		for ch in str_ans[::-1]:
			if ch != '0':
				return ch


class Solution:

	def lastNon0Digit(self, N):
		pass

		def fact(n):
			if n == 1 or n == 0:
				return 1
			return n * fact(n - 1)
		f = fact(N)
		r = f % 10
		while not r != 0:
			f = f // 10
			r = f % 10
		return r


class Solution:

	def lastNon0Digit(self, N):
		c = 1
		for i in range(1, N + 1):
			c = c * i
		digit = 0
		while c > 0:
			digit = c % 10
			if digit != 0:
				return digit
			c = c // 10
		return -1


class Solution:

	def lastNon0Digit(self, N):
		fact = 1
		for i in range(1, N + 1):
			fact *= i
		while fact > 0:
			rem = fact % 10
			if rem != 0:
				return rem
			fact //= 10
		return 0


class Solution:

	def lastNon0Digit(self, N):
		pass
		ans = self.factorial(N)
		res = self.getNumber(ans)
		return res

	def factorial(self, N):
		if N == 1:
			return N
		return N * self.factorial(N - 1)

	def getNumber(self, N):
		if N == 0:
			return
		if N % 10 > 0:
			return N % 10
		return self.getNumber(N // 10)


class Solution:

	def lastNon0Digit(self, N):
		pass
		fact = 1
		for i in range(1, N + 1):
			fact = fact * i
		fact = str(fact)
		x = list(fact)
		x = x[::-1]
		for i in range(N):
			if int(x[i]) != 0:
				return int(x[i])


class Solution:

	def lastNon0Digit(self, N):
		pass

		def Fact(n):
			if n == 0:
				return 1
			else:
				return n * Fact(n - 1)
		result = Fact(N)
		while result:
			if result % 10 == 0:
				result = result // 10
			else:
				return result % 10


class Solution:

	def lastNon0Digit(self, N):
		factorial = self.fact(N)
		return self.modulo(factorial)

	def modulo(self, n):
		if n % 10 != 0:
			return n % 10
		return self.modulo(n // 10)

	def fact(self, n):
		if n == 1:
			return 1
		return n * self.fact(n - 1)


import math as m

class Solution:

	def lastNon0Digit(self, N):
		z = str(m.factorial(N))
		l = list(z)
		k = [int(i) for i in l]
		k = k[::-1]
		for i in k:
			if i != 0:
				return i


class Solution:

	def lastNon0Digit(self, N):

		def fact(n):
			if n == 1:
				return 1
			return n * fact(n - 1)
		result = fact(N)
		while result:
			if result % 10 == 0:
				result = result // 10
			else:
				return result % 10


class Solution:

	def findFactorial(self, n):
		if n == 0:
			return 1
		return n * self.findFactorial(n - 1)

	def lastNon0Digit(self, n):
		fact = self.findFactorial(n)
		ans = 0
		s_fact = str(fact)
		res = s_fact.replace('0', '')
		return int(res[-1])


import math

class Solution:

	def lastNon0Digit(self, N):
		x = math.factorial(N)
		c = 0
		while N >= 1:
			y = N // 5
			c += y
			N = N // 5
		s = str(x)
		return s[len(s) - 1 - c]


class Solution:

	def fact(self, n):
		if n == 1:
			return 1
		return n * self.fact(n - 1)

	def lastNon0Digit(self, N):
		ans = self.fact(N)
		for i in str(ans)[::-1]:
			if i != '0':
				return i


class Solution:

	def lastNon0Digit(self, N):

		def fa(x):
			if x == 0:
				return 1
			else:
				return x * fa(x - 1)
		r = fa(N)
		while r:
			if r % 10 == 0:
				r = r // 10
			else:
				return r % 10


import math

class Solution:

	def lastNon0Digit(self, N):
		val = str(math.factorial(N))
		for i in range(len(val) - 1, -1, -1):
			if int(val[i]) != 0:
				return int(val[i])


class Solution:

	def lastNon0Digit(self, N):
		pass

		def f(n):
			if n == 1:
				return n
			return n * f(n - 1)
		l = []
		for i in str(f(N)):
			if i == '0':
				continue
			else:
				l.append(int(i))
		return l[-1]


import sys
sys.setrecursionlimit(1000001)

def fact(n):
	if n == 0:
		return 1
	return n * fact(n - 1)

class Solution:

	def lastNon0Digit(self, N):
		ans = fact(N)
		while ans:
			num = ans % 10
			if num != 0:
				return num
			ans //= 10
		return -1


class Solution:

	def lastNon0Digit(self, N):
		pass
		if N == 1:
			return 1
		elif N > 1:
			fact = 1
			for i in range(1, N + 1):
				fact = fact * i
			x = list(str(fact))
			for i in range(len(x) - 1, -1, -1):
				if x[i] != '0':
					return x[i]
					break
		else:
			return 0


import math

class Solution:

	def lastNon0Digit(self, N):
		pass
		N = math.factorial(N)
		while N:
			if N % 10 != 0:
				return N % 10
			N = N // 10


class Solution:

	def ft(self, a):
		if a == 1:
			return 1
		return a * self.ft(a - 1)

	def lastNon0Digit(self, N):
		k = self.ft(N)
		s = str(k)
		l = list(s)
		for i in l[::-1]:
			if i != '0':
				return int(i)


class Solution:

	def lastNon0Digit(self, n):
		pass
		k = self.fact(n)
		l = str(k)
		ans = l[::-1]
		for i in ans:
			if i != '0':
				return i
			else:
				continue

	def fact(self, n):
		if n == 0 or n == 1:
			return 1
		else:
			return n * self.fact(n - 1)


import math

class Solution:

	def lastNon0Digit(self, N):
		pass
		f = math.factorial(N)
		k = list(map(int, str(f)))
		k = k[::-1]
		for i in range(len(k)):
			if k[i] != 0:
				return k[i]
				break
		return -1


class Solution:

	def lastNon0Digit(self, N):
		return self.NonZero(str(self.fact(N)))

	def fact(self, n):
		if n == 1:
			return n
		return n * self.fact(n - 1)

	def NonZero(self, s, size=1):
		if s[-1] != '0':
			return s[-1]
		size = len(s) - 1
		return self.NonZero(s[:size], size)


class Solution:

	def lastNon0Digit(self, N):
		pass
		s = 1
		for i in range(1, N + 1):
			s *= i
		s = str(s)
		s = s[::-1]
		for i in s:
			if i != '0':
				return int(i)


from math import factorial

class Solution:

	def lastNon0Digit(self, N):
		x = factorial(N)
		l = []
		while x != 0:
			rem = x % 10
			if rem == 0:
				pass
			else:
				return rem
			x = x // 10


import math

class Solution:

	def lastNon0Digit(self, N):
		a = math.factorial(N)
		k = str(a)[::-1]
		for i in k:
			if i != '0':
				return i


class Solution:

	def lastNon0Digit(self, N):
		fact = 1
		while N:
			fact = fact * N
			N -= 1
		rem = 0
		while not rem:
			rem = fact % 10
			fact //= 10
		return rem


import math

class Solution:

	def lastNon0Digit(self, N):
		ans = math.factorial(N)
		s = str(ans)
		p = s[::-1]
		for i in p:
			if i != '0':
				res = i
				break
		return res


class Solution:

	def lastNon0Digit(self, n):
		pass

		def f(n):
			if n == 0:
				return 1
			return n * f(n - 1)
		a = f(n)
		c = list(str(a))
		for i in range(c.count('0')):
			c.remove('0')
		return c[-1]


def fact(n):
	if n == 0:
		return 1
	return n * fact(n - 1)

class Solution:

	def lastNon0Digit(self, N):
		f = fact(N)
		last = 0
		while f != 0:
			last = f % 10
			if last != 0:
				break
			f = f // 10
		return last
		pass


class Solution:

	def lastNon0Digit(self, N):
		p = 1
		for x in range(2, N + 1):
			p *= x
		while not p % 10:
			p //= 10
		return p % 10


import math

class Solution:

	def lastNon0Digit(self, N):

		def fun(n, re):
			if re != 0:
				return int(re)
			re = n % 10
			n = n // 10
			return fun(n, re)
		n = math.factorial(N)
		return fun(n, 0)


import math

class Solution:

	def lastNon0Digit(self, N):
		pass
		x = str(math.factorial(N))
		x = x[::-1]
		for i in x:
			if i != '0':
				return i


def fact(N):
	if N == 1:
		return 1
	c = N * fact(N - 1)
	while c % 10 == 0:
		c = c // 10
	return c

class Solution:

	def lastNon0Digit(self, N):
		m = fact(N)
		return m % 10


class Solution:

	def lastNon0Digit(self, N):
		pass
		l = []
		res = 1
		for i in range(2, N + 1):
			res = res * i
		while res:
			if res % 10 == 0:
				res = res // 10
			else:
				return res % 10


from math import factorial

class Solution:

	def lastNon0Digit(self, N):
		num = factorial(N)
		while num % 10 == 0:
			num = num // 10
		return num % 10


def fact(n):
	if n == 0:
		return 1
	return n * fact(n - 1)

class Solution:

	def lastNon0Digit(self, N):
		pass
		n = fact(N)
		while n > 0:
			r = n % 10
			if r != 0:
				return r
			n = n // 10


class Solution:

	def fact(self, n):
		if n <= 1:
			return 1
		return n * self.fact(n - 1)

	def lastNon0Digit(self, N):
		a = self.fact(N)
		while True:
			rem = a % 10
			if rem != 0:
				return rem
			a //= 10
		return a


class Solution:

	def facc(self, n):
		if n == 1:
			return 1
		return n * self.facc(n - 1)

	def lastNon0Digit(self, N):
		a = self.facc(N)
		while a:
			d = a % 10
			if d != 0:
				return d
			a = a // 10


class Solution:

	def lastNon0Digit(self, N):
		f = 1
		while N != 0:
			f *= N
			N -= 1
		while True:
			if f % 10 != 0:
				return f % 10
			else:
				f = f // 10


class Solution:

	def lastNon0Digit(self, N):

		def factorial(n):
			res = 1
			for i in range(1, n + 1):
				res = res * i
			return res
		t = factorial(N)
		while t > 0:
			if t % 10 != 0:
				return t % 10
			t = t // 10


def factorial(n):
	ans = 1
	for i in range(1, n + 1):
		ans *= i
	return ans

class Solution:

	def lastNon0Digit(self, N):
		ans = factorial(N)
		s = str(ans)[::-1]
		while ans:
			r = ans % 10
			ans = ans // 10
			if r != 0:
				return r
				break


class Solution:

	def lastNon0Digit(self, N):

		def fac(n):
			if n == 0:
				return 1
			else:
				return n * fac(n - 1)
		N = fac(N)
		while N % 10 == 0:
			N //= 10
		return N % 10


from math import factorial

class Solution:

	def lastNon0Digit(self, N):
		pass
		N = factorial(N)
		while N % 10 == 0:
			N //= 10
		else:
			return N % 10


class Solution:

	def lastNon0Digit(self, N):
		pass

		def fact(n):
			if n == 0:
				return 1
			return n * fact(n - 1)
		ans = fact(N)
		while ans:
			if ans % 10 == 0:
				ans = ans // 10
			else:
				return ans % 10


class Solution:

	def lastNon0Digit(self, N):

		def helper(N):
			if N == 1:
				return 1
			return N * helper(N - 1)
		ans = helper(N)
		while ans % 10 == 0:
			ans //= 10
		return ans % 10


import math

class Solution:

	def lastNon0Digit(self, N):
		pass
		l = str(math.factorial(N))
		s = l.replace('0', '')
		return s[-1]


class Solution:

	def lastNon0Digit(self, n):
		fact = 1
		for i in range(1, n + 1):
			fact = fact * i
		while fact % 10 == 0:
			fact = fact // 10
		return fact % 10


class Solution:

	def lastNon0Digit(self, N):
		pass
		a = 1
		if N == 0:
			return 1
		for i in range(1, N + 1):
			a = a * i
		while a % 10 == 0:
			a = a // 10
		return a % 10


class Solution:

	def lastNon0Digit(self, N):
		factor = 1
		for i in range(1, N + 1):
			factor *= i
		sfactor = str(factor)
		number = ''
		for i in range(len(sfactor)):
			if sfactor[i] != '0':
				number += sfactor[i]
		a = len(number)
		if a == 1:
			return int(number[0])
		else:
			return int(number[a - 1])


class Solution:

	def lastNon0Digit(self, N):
		fact = 1
		for i in range(1, N + 1):
			fact = fact * i
		A = str(fact)
		n = len(A)
		for i in range(n):
			if A[n - i - 1] == '0':
				continue
			else:
				return int(A[n - i - 1])


class Solution:

	def lastNon0Digit(self, N):
		pass
		fact = 1
		cnt = 0
		for i in range(1, N + 1):
			fact = fact * i
			while fact % 10 == 0:
				fact = fact / 10
				fact %= 1000000000
		return int(fact % 10)


class Solution:

	def facto(self, n):
		if n == 1:
			return 1
		return n * self.facto(n - 1)

	def lastNon0Digit(self, N):
		x = self.facto(N)
		while x % 10 == 0:
			x //= 10
		return x % 10


class Solution:

	def lastNon0Digit(self, N):
		b = []
		mul = 1
		for i in range(1, N + 1):
			mul *= i
		for i in str(mul):
			b.append(int(i))
		a = b[-1::-1]
		for i in range(len(a)):
			if a[i] != 0:
				return a[i]
				break


class Solution:

	def lastNon0Digit(self, N):
		pass
		import math
		s = str(math.factorial(N))[::-1]
		s = str(int(s))
		return s[0]


import math

class Solution:

	def lastNon0Digit(self, N):
		n = str(int(math.factorial(N)))
		for i in n[::-1]:
			if int(i) != 0:
				return int(i)


class Solution:

	def lastNon0Digit(self, N):

		def fact(n):
			if n == 1:
				return 1
			else:
				return n * fact(n - 1)
		facto = fact(N)
		t = facto
		while t != 0:
			d = t % 10
			if d != 0:
				return d
			t = t // 10


import math

class Solution:

	def lastNon0Digit(self, N):
		pass
		ans = str(math.factorial(N))
		for i in range(len(ans) - 1, -1, -1):
			if ans[i] != '0':
				return ans[i]


class Solution:

	def lastNon0Digit(self, n):
		pass
		p = n
		for i in range(2, n):
			p = p * i
		while p:
			r = p % 10
			if r != 0:
				return r
			p //= 10


class Solution:

	def lastNon0Digit(self, N):

		def helper(N):
			if N == 0:
				return 1
			else:
				return N * helper(N - 1)
		a = helper(N)
		while a:
			if a % 10 == 0:
				a = a // 10
			else:
				return a % 10


dig = [1, 1, 2, 6, 4, 2, 2, 4, 2, 8]

class Solution:

	def lastNon0Digit(self, N):
		pass
		n = N
		if n < 10:
			return dig[n]
		if n // 10 % 10 % 2 == 0:
			return 6 * self.lastNon0Digit(n // 5) * dig[n % 10] % 10
		else:
			return 4 * self.lastNon0Digit(n // 5) * dig[n % 10] % 10
		return 0


class Solution:

	def lastNon0Digit(self, N):
		res = 1
		for i in range(1, N + 1):
			res *= i
			while res % 10 == 0:
				res //= 10
		return res % 10


class Solution:

	def lastNon0Digit(self, N):
		fact1 = 1
		for i in range(1, N + 1):
			fact1 = fact1 * i

		def func1(fact, dig):
			if dig != 0:
				return dig
			else:
				return func1(fact // 10, fact % 10)
		fact = func1(fact1, fact1 % 10)
		return fact


import math

class Solution:

	def lastNon0Digit(self, N):
		f = math.factorial(N)
		while f:
			if f % 10:
				return f % 10
			f //= 10


class Solution:

	def factorial(self, N):
		if N == 1:
			return N
		f = self.factorial(N - 1)
		return N * f

	def lastNon0Digit(self, N):
		pass
		f = self.factorial(N)
		while f:
			rem = f % 10
			if rem == 0:
				f = f // 10
			else:
				return rem


class Solution:

	def lastNon0Digit(self, N: int) -> int:

		def factorial(n: int) -> int:
			result = 1
			for i in range(n, 1, -1):
				result *= i
			return result
		number = factorial(N)
		while number:
			digit = number % 10
			if digit:
				return digit
			number //= 10


class Solution:

	def lastNon0Digit(self, N):
		fact = factorial(N)
		s = str(fact)
		for x in s[-1::-1]:
			if x == '0':
				continue
			else:
				return x

def factorial(n):
	if n == 1 or n == 0:
		return 1
	return factorial(n - 1) * n


class Solution:

	def rec(self, n, dp):
		if n == 0 or n == 1:
			dp[n] = 1
			return 1
		elif dp[n] != 0:
			return dp[n]
		else:
			dp[n] = n * self.rec(n - 1, dp)
			return dp[n]

	def lastNon0Digit(self, N):
		dp = [0] * (N + 1)
		result = self.rec(N, dp)
		for i in range(len(str(result)) - 1, -1, -1):
			if str(result)[i] != '0':
				return int(str(result)[i])


class Solution:

	def lastNon0Digit(self, N):
		s = str(self.fact(N))
		s = s.replace('0', '')
		return s[-1]

	def fact(self, n):
		if n == 1 or n == 0:
			return 1
		f = self.fact(n - 1) * n
		return f


class Solution:

	def lastNon0Digit(self, N):

		def Fact(n):
			if n == 0:
				return 1
			else:
				return n * Fact(n - 1)
		ans = Fact(N)
		while ans:
			if ans % 10 == 0:
				ans //= 10
			else:
				return ans % 10


import math

class Solution:

	def lastNon0Digit(self, N):
		fact = str(math.factorial(N))
		last_non_zero_digit = -1
		for i in range(len(fact) - 1, -1, -1):
			num = int(fact[i])
			if num != 0:
				last_non_zero_digit = num
				break
		return last_non_zero_digit
		pass


class Solution:

	def lastNon0Digit(self, N):
		pass
		x = 1
		while N != 0:
			x = x * N
			N = N - 1
		while x != 0:
			y = x % 10
			if y == 0:
				x = x // 10
			else:
				break
		return y


class Solution:

	def lastNon0Digit(self, N):

		def fac(n):
			if n == 1:
				return 1
			else:
				return n * fac(n - 1)
		t = str(fac(N))
		for i in range(len(t) - 1, -1, -1):
			if t[i] != '0':
				return t[i]


class Solution:

	def lastNon0Digit(self, N):
		product = 1
		for i in range(1, N + 1, 1):
			product = product * i
		ans = str(product)
		ans = ans[::-1]
		product = int(ans)
		ans = str(product)
		return int(ans[0])


class Solution:

	def lastNon0Digit(self, N):
		result = 1
		for i in range(1, N + 1):
			result *= i
		while result:
			if result % 10 == 0:
				result = result // 10
			else:
				return result % 10


class Solution:

	def lastNon0Digit(self, N):

		def rec(n):
			if n == 0 or n == 1:
				return 1
			else:
				return n * rec(n - 1)
		result = rec(N)
		while result % 10 == 0:
			result = result // 10
		return result % 10


class Solution:

	def lastNon0Digit(self, n):
		pass
		w = 1
		x = n + 1
		for i in range(1, x):
			w = w * i
		i = 0
		while i < n:
			s = w % 10
			if s != 0:
				break
			i = i + 1
			w = w // 10
		return s