📌  相关文章
📜  N的基数16表示中的尾随零数!

📅  最后修改于: 2021-06-25 17:04:37             🧑  作者: Mango

给定整数N ,任务是在N的阶乘16的基数表示中找到尾随零的数量。
例子:

方法:

  • 尾随零的数量将是以10N阶乘中16的最高幂。
  • 我们知道16 = 2 4 。因此,在N的阶乘中,最高功率16等于最高功率2除以4
  • 要计算N2的最大幂 ,我们可以使用勒让德公式。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#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


Javascript


输出:
1

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。