Working on an example but it does not work. There must be a function which counts trailing zero in n! the factorial formula. where, n is the number for whose factorial we want to find the number of trailing zeros. Used some imports but it did not work.
My code:
def is_positive_integer(x): try: x = float(x) except ValueError: return False else: if x.is_integer() and x > 0: return True else: return False def trailing_zeros(num): if is_positive_integer(num): # The above function call has done all the sanity checks for us # so we can just convert this into an integer here num = int(num) k = math.floor(math.log(num, 5)) zeros = 0 for i in range(1, k + 1): zeros = zeros + math.floor(num/math.pow(5, i)) return zeros else: print("Factorial of a non-positive non-integer is undefined")
Ex:
Input: n = 5 Output: 1 Factorial of 5 is 120 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
Output must be:
Trailing 0s in n! = Count of 5s in prime factors of n! = floor(n/5) + floor(n/25) + floor(n/125) + ....
Advertisement
Answer
This code can do the job, your code is very complex.
def Zeros(n): count = 0 i = 5 while n / i >= 1: count += int(n / i) i *= 5 return int(count) n = 100 print("Trailing zeros " + "in 100! is", Zeros(n))
Output:
Trailing zeros in 100! is 24