Dado un número entero N , la tarea es encontrar el número de ceros finales en la representación en base 16 del factorial de N .
Ejemplos:
Entrada: N = 6
Salida: 1
6! = 720 (base 10) = 2D0 (base 16)
Entrada: N = 100
Salida: 24
Acercarse:
- El número de ceros finales sería la potencia más alta de 16 en el factorial de N en base 10 .
- Sabemos que 16 = 2 4 . Entonces, la potencia más alta de 16 es igual a la potencia más alta 2 en el factorial de N dividido por 4 .
- Para calcular la mayor potencia de 2 en N! , podemos usar la fórmula de Legendre .
A continuación se muestra la implementación del enfoque anterior:
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); }
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.
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 = 6 print(getTrailingZeroes(n)) # This code is contributed # by Mohit Kumar
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
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. ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the count of trailing zeroes function 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>
Producción:
1