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 intusing namespace std;// Function to return the count of trailing zeroesll 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 codeint main(){ int n = 6; cout << getTrailingZeroes(n);} |
Java
// Java implementation of the approachclass GfG{// Function to return the count of trailing zeroesstatic 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 codepublic static void main(String[] args){ int n = 6; System.out.println(getTrailingZeroes(n));}}// This code is contributed by// Prerna Saini. |
Python3
# Python3 implementation of the approach# Function to return the count of# trailing zeroesdef 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 coden = 6print(getTrailingZeroes(n))# This code is contributed# by Mohit Kumar |
C#
// C# implementation of the approachusing System;class GFG{// Function to return the count of// trailing zeroesstatic 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 codepublic static void Main(){ int n = 6; Console.Write(getTrailingZeroes(n));}}// This code is contributed by// Akanksha Rai |
PHP
<?php// PHP implementation of the approach// Function to return the count of// trailing zeroesfunction 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.?> |
Javascript
<script>// JavaScript implementation of the approach// Function to return the count of trailing zeroesfunction getTrailingZeroes(n){ let count = 0; let val, powerTwo = 2; // Implementation of the Legendre's formula do { val = Math.floor(n / powerTwo); count += val; powerTwo *= 2; } while (val != 0); // Count has the highest power of 2 // that divides n! in base 10 return (Math.floor(count / 4));}// Driver code let n = 6; document.write(getTrailingZeroes(n));// This code is contributed by Surbhi Tyagi</script> |
1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


