Given an integer N, the task is to find the number of trailing zeroes in the base 16 representation of the factorial of N.
Examples:
Input: N = 6
Output: 1
6! = 720 (base 10) = 2D0 (base 16)Input: N = 100
Output: 24
Approach:
- Number of trailing zeroes would be the highest power of 16 in the factorial of N in base 10.
- We know that 16 = 24. So, the highest power of 16 is equal to the highest power 2 in the factorial of N divided by 4.
- To calculate the highest power of 2 in N!, we can use Legendre’s Formula.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> #define ll long long int using namespace std; // Function to return the count of trailing zeroes ll getTrailingZeroes(ll n) { ll count = 0; ll val, powerTwo = 2; // Implementation of the Legendre's formula do { val = n / powerTwo; count += val; powerTwo *= 2; } while (val != 0); // Count has the highest power of 2 // that divides n! in base 10 return (count / 4); } // Driver code int main() { int n = 6; cout << getTrailingZeroes(n); } |
chevron_right
filter_none
Java
// Java implementation of the approach class GfG { // Function to return the count of trailing zeroes static long getTrailingZeroes(long n) { long count = 0; long val, powerTwo = 2; // Implementation of the Legendre's formula do { val = n / powerTwo; count += val; powerTwo *= 2; } while (val != 0); // Count has the highest power of 2 // that divides n! in base 10 return (count / 4); } // Driver code public static void main(String[] args) { int n = 6; System.out.println(getTrailingZeroes(n)); } } // This code is contributed by // Prerna Saini. |
chevron_right
filter_none
Python3
# Python3 implementation of the approach # Function to return the count of # trailing zeroes def getTrailingZeroes(n): count = 0 val, powerTwo = 1, 2 # Implementation of the Legendre's # formula while (val != 0): val = n //powerTwo count += val powerTwo *= 2 # Count has the highest power of 2 # that divides n! in base 10 return (count // 4) # Driver code n = 6print(getTrailingZeroes(n)) # This code is contributed # by Mohit Kumar |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { // Function to return the count of // trailing zeroes static long getTrailingZeroes(long n) { long count = 0; long val, powerTwo = 2; // Implementation of the // Legendre's formula do { val = n / powerTwo; count += val; powerTwo *= 2; } while (val != 0); // Count has the highest power of 2 // that divides n! in base 10 return (count / 4); } // Driver code public static void Main() { int n = 6; Console.Write(getTrailingZeroes(n)); } } // This code is contributed by // Akanksha Rai |
chevron_right
filter_none
PHP
<?php // PHP implementation of the approach // Function to return the count of // trailing zeroes function getTrailingZeroes($n) { $count = 0; $val; $powerTwo = 2; // Implementation of the Legendre's formula do { $val = (int)($n / $powerTwo); $count += $val; $powerTwo *= 2; } while ($val != 0); // Count has the highest power of 2 // that divides n! in base 10 return ($count / 4); } // Driver code $n = 6; echo(getTrailingZeroes($n)); // This code is contributed by // Code_Mech. ?> |
chevron_right
filter_none
Output:
1
Recommended Posts:
- Number of trailing zeroes in base B representation of N!
- Count trailing zeroes in factorial of a number
- Smallest number with at least n trailing zeroes in factorial
- Trailing number of 0s in product of two factorials
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Golang Program to Count Trailing Zeros in Factorial of a Number
- Move all zeroes to end of array using Two-Pointers
- Find the length of factorial of a number in any given base
- Count of primes after converting given binary number in base between L to R
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Next greater number than N with exactly one bit different in binary representation of N
- Number of mismatching bits in the binary representation of two integers
- Find consecutive 1s of length >= n in binary representation of a number
- Sum of digits with even number of 1's in their binary representation
- Maximum number of consecutive 1's in binary representation of all the array elements
- Get the kth smallest number using the digits of the given number
- Number of steps required to convert a binary number to one
- Number of compositions of a natural number
- Number of digits in the nth number made of given four digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


