Given two positive integers B and N. The task is to find the number of trailing zeroes in b-ary (base B) representation of N! (factorial of N)
Input: N = 5, B = 2 Output: 3 5! = 120 which is represented as 1111000 in base 2. Input: N = 6, B = 9 Output: 1
A naive solution is to find the factorial of the given number and convert it into given base B. Then, count the number of trailing zeroes but that would be a costly operation. Also, it will not be easy to find the factorial of large numbers and store it in integer.
Efficient Approach: Suppose, the base is 10 i.e., decimal then we’ll have to calculate the highest power of 10 that divides N! using Legendre’s formula. Thus, number B is represented as 10 when converted into base B. Let’s say base B = 13, then 13 in base 13 will be represented as 10, i.e., 1310 = 1013. Hence, problem reduces to finding the highest power of B in N!. (Largest power of k in n!)
Below is the implementation of the above approach.
# Python 3 program to find the number of
# trailing zeroes in base B representation of N!
# To find the power of a prime
# p in factorial N
def findPowerOfP(N, p):
count = 0
r = p
while (r <= N): # calculating floor(n/r) # and adding to the count count += int(N / r) # increasing the power of p # from 1 to 2 to 3 and so on r = r * p return count # returns all the prime factors of k def primeFactorsofB(B): # vector to store all the prime factors # along with their number of occurrence # in factorization of B' ans =  i = 2 while(B!= 1): if (B % i == 0): count = 0 while (B % i == 0): B = int(B / i) count += 1 ans.append((i, count)) i += 1 return ans # Returns largest power of B that # divides N! def largestPowerOfB(N, B): vec =  vec = primeFactorsofB(B) ans = sys.maxsize # calculating minimum power of all # the prime factors of B ans = min(ans, int(findPowerOfP(N, vec) / vec)) return ans # Driver code if __name__ == '__main__': print(largestPowerOfB(5, 2)) print(largestPowerOfB(6, 9)) # This code is contributed by # Surendra_Gangwar [tabbyending]
- Number of trailing zeroes in base 16 representation of N!
- Count trailing zeroes in factorial of a number
- Smallest number with at least n trailing zeroes in factorial
- Count number of trailing zeros in Binary representation of a number using Bitset
- Given a number N in decimal base, find number of its digits in any base (base b)
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Trailing number of 0s in product of two factorials
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Smallest number divisible by n and has at-least k trailing zeros
- Count number of trailing zeros in product of array
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Pandigital number in a given base
- Complement of a number with any base b
- Check if a number N starts with 1 in b-base
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SURENDRA_GANGWAR