Atul is frustrated with large digits factorials so he decided to convert the factorials in power of 10  ( 120 ->   1* 10^2 ). He is not good in programming so he is asking for your help to know the first digit of factorial and power of 10.  
For ex.  5 (120 ->   1* 10^2)   first digit = 1
                                               power     = 2
 
Example 1:
Ã¢â¬â¹Input : N = 5
Output : 1 0
Explanation:
Factorial of 5 = 120
So, first digit = 1 and power of 10 = 2
(1 * 10^{2})
Ã¢â¬â¹Example 2:
Input : N = 9
Output :  3 5 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function fact() that takes an integer N and return the first digit of factorial and power of 10. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
 
Constraints
1 ≤ N≤ 2*10^{4}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Mathematics

Starter code: #User function Template for python3



def fact (n) : 

    #Complete the function

    



Solutions:
def fact(n):
	st = 1
	while n:
		st *= n
		n -= 1
	return [int(str(st)[0]), len(str(st)) - 1]


import math

def fact(n):
	d = math.factorial(n)
	return (str(d)[0], len(str(d)) - 1)


import math

def fact(n):
	s = 0.0
	for i in range(1, n + 1):
		s += math.log10(i)
	ans = [0, 0]
	f = math.floor(s)
	p = s - f
	ans[0] = int(math.pow(10, p) + 1e-06)
	ans[1] = f
	return ans


import math

def fact(n):
	f = math.factorial(n)
	return [str(f)[:1], len(str(f)) - 1]


import math

def fact(n):
	x = str(math.factorial(n))
	arr = list(x)
	count = len(arr)
	return (arr[0], count - 1)


import sys
sys.setrecursionlimit(1000001)

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

def fact(n):
	res = str(fac(n))
	return (int(res[0]), len(res) - 1)


arr = [1]

def fact(n):
	while n >= len(arr):
		arr.append(arr[-1] * len(arr))
	num = str(arr[n])
	return [num[0], len(num) - 1]


def fact(n):
	x = 1
	for i in range(1, n + 1):
		x = x * i
	r = str(x)
	arr = list(r)
	count = len(arr)
	return (arr[0], count - 1)


l = [1]
m = 2 * 10 ** 4
for i in range(1, m + 1):
	l.append(i * l[i - 1])

def fact(n):
	a = str(l[n])
	f = a[0]
	t = len(a) - 1
	return [f, t]


def fact(n):
	f = 1
	if n == 0:
		f = 1
	else:
		for i in range(1, n + 1):
			f = f * i
	if len(str(f)) == 1:
		f = str(f)
		return [int(f[0]), 0]
	else:
		f = str(f)
		return [int(f[0]), len(f) - 1]


import math

def fact(n):
	b = math.factorial(n)
	c = math.floor(math.log10(b) + 1)
	d = []
	e = b // pow(10, c - 1)
	d.append(e)
	d.append(c - 1)
	return d


import math

def fact(n):
	sn = str(math.factorial(n))
	return (int(sn[0]), len(sn) - 1)


def fact(n):
	f = 1
	for i in range(1, n + 1):
		f = f * i
	x = f // 10
	f = str(f)
	return (f[0], len(f) - 1)


def fact(n):
	import math
	ans = str(math.factorial(n))
	return (ans[0], len(ans) - 1)


def fact(n):
	fac = 1
	for i in range(1, n + 1):
		fac *= i
	fac = str(fac)
	return (fac[0], len(fac) - 1)


def fact(n):
	mul = 1
	for i in range(1, n + 1):
		mul *= i
	mul = str(mul)
	a = int(mul[0])
	b = len(mul[1:])
	return (a, b)


def fact(n):
	f = 1
	for i in range(1, n + 1):
		f *= i
	p = str(f)
	s = len(p) - 1
	r = p[0]
	return [r, s]


def fact(n):
	p = 0
	f = 1
	for i in range(2, n + 1):
		f *= i
	while f > 9:
		f = f // 10
		p += 1
	return (f, p)


def fact(n):
	p = 1
	c = 0
	for i in range(1, n + 1):
		p = p * i
		while p % 10 == 0:
			c += 1
			p = p // 10
	j = str(p)
	return [j[0], c + len(j) - 1]


import math

def fact(n):
	s = str(math.factorial(n))
	l = list(s)
	c = len(l)
	return (l[0], c - 1)


def fact(n):

	def factorial(n):
		res = 1
		for i in range(2, n + 1):
			res *= i
		return res
	f = factorial(n)
	frst_dgt = 0
	pow_of_ten = 0
	while f != 0:
		t = f
		f //= 10
		pow_of_ten += 1
	frst_dgt = t
	res = [frst_dgt, pow_of_ten - 1]
	return res


import math

def fact(n):
	x = str(math.factorial(n))
	arr = list(x)
	count = len(arr)
	return (arr[0], count - 1)
for _ in range(0, int(input())):
	n = int(input())
	ans = fact(n)
	print(*ans)


def fact(n):
	fact = 1
	r = []
	for i in range(1, n + 1):
		fact = fact * i
	a = str(fact)
	m = int(a[0])
	r.append(m)
	r.append(len(a) - 1)
	return r
for _ in range(0, int(input())):
	n = int(input())
	ans = fact(n)
	print(*ans)


import math

def fact(n):
	s = str(math.factorial(n))
	l = list(s)
	c = len(l)
	return (l[0], c - 1)
for _ in range(0, int(input())):
	n = int(input())
	ans = fact(n)
	print(*ans)


def fact(n):
	dp = {}
	dp[1] = 1
	dp[2] = 2
	dp[3] = 6
	for i in range(4, n + 1):
		dp[i] = i * dp[i - 1]
	u = len(str(dp[n]))
	return (dp[n] // 10 ** (u - 1), u - 1)
for _ in range(0, int(input())):
	n = int(input())
	ans = fact(n)
	print(*ans)


def fact(n):
	x = 1
	for i in range(1, n + 1):
		x *= i
	a = str(x)[0]
	b = len(str(x)) - 1
	return (a, b)


def fact(n):
	fact = 1
	arr = []
	l = []
	for i in range(1, n + 1):
		fact *= i
	while fact:
		arr.append(fact % 10)
		fact = fact // 10
	l.append(arr[-1])
	l.append(len(arr) - 1)
	return l


def fact(n):
	fact = 1
	for i in range(1, n + 1):
		fact = fact * i
	l = [*map(str, str(fact).strip())]
	k = len(l) - 1
	return [l[0], k]


import math

def fact(n):
	x = []
	l = math.factorial(n)
	z = str(l)
	return (z[0], len(z) - 1)


def fact(n):
	p = 1
	for i in range(1, n + 1):
		p *= i
	l = len(str(p))
	first = int(str(p)[0])
	return (first, l - 1)


def fact(n):
	x = 1
	for i in range(1, n + 1):
		x = x * i
	s = str(x)
	t = int(s[0])
	q = len(s) - 1
	return (t, q)


import math

def fact(n):
	k = []
	a = str(math.factorial(n))
	k.append(a[0])
	k.append(len(a) - 1)
	return k


import math

def fact(n):
	for i in range(n):
		fac = str(math.factorial(n))
	return (fac[0], len(fac) - 1)


def fact(n):
	number = 1
	for i in range(1, n + 1):
		number = number * i
	string = str(number)
	return [int(string[0]), len(string) - 1]


def fact(n):
	x = 1
	for i in range(2, n + 1):
		x = x * i
	x = str(x)
	return [int(x[0:1]), len(x[1:])]


def fact(n):
	arr = [1]
	for i in range(1, n + 1):
		arr.append(arr[-1] * i)
	return [str(arr[-1])[0], len(str(arr[-1])) - 1]


import math

def fact(n):
	fac = str(math.factorial(n))
	return (fac[0], len(fac) - 1)


def fact(n):
	import math
	facto = math.factorial(n)
	strnum = str(facto)
	(b, c) = (int(strnum[0]), len(strnum) - 1)
	return [b, c]


def fact(n):
	val = 1
	while n > 0:
		val *= n
		n = n - 1
	l = list(map(int, str(val)))
	return (l[0], len(l) - 1)


import math

def fact(n):
	k = math.factorial(n)
	j = [int(i) for i in str(k)]
	l = [j[0], len(j) - 1]
	return l


import math

def fact(n):
	f = math.factorial(n)
	t = 0
	while f // 10 > 0:
		t = t + 1
		f = f // 10
	return (str(f)[0], t)


import math

def fact(n):
	x = 1
	for i in range(1, n + 1):
		x = x * i
	a = str(x)
	b = int(a[0])
	c = len(a) - 1
	return (b, c)


def fact(n):
	s = 1
	for i in range(1, n + 1):
		s = s * i
	p1 = str(s)
	c = p1[0]
	c1 = int(c)
	a = len(p1)
	p = a - 1
	q = c1
	return [q, p]


def fact(n):
	l = []
	N = 1
	for i in range(1, n + 1):
		N = N * i
	N = str(N)
	l.append(int(N[0]))
	l.append(len(N) - 1)
	return l


from math import log10

def fact(n):
	fact_n = 1
	for i in range(2, n + 1):
		fact_n = fact_n * i
	power_10 = int(log10(fact_n))
	first_dgt = fact_n // 10 ** power_10
	return (first_dgt, power_10)


def fact(n):
	if n < 0:
		fact = 0
	elif n == 0 or n == 1:
		fact = 1
	else:
		fact = 1
		while n > 1:
			fact *= n
			n -= 1
	n1 = fact
	n2 = str(fact)
	l = []
	l.append(int(n2[0]))
	for i in range(1, 10000):
		if 10 ** i > n1:
			l.append(i - 1)
			break
	return l