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.
- Number of trailing zeroes in base 16 representation of N!
- Smallest number with at least n trailing zeroes in factorial
- Count trailing zeroes in factorial of a number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count of N-digit numbers in base K with no two consecutive zeroes
- Given a number N in decimal base, find number of its digits in any base (base b)
- Find the Nth digit from right in base B of the given number in Decimal base
- Given a number N in decimal base, find the sum of digits in any base B
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Trailing number of 0s in product of two factorials
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- 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
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count number of trailing zeros in product of array
- Golang Program to Count Trailing Zeros in Factorial of a Number
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Add two integers of different base and represent sum in smaller base of the two
- Complement of a number with any base b
- Pandigital number in a given 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.