📜  计算数字阶乘中的尾随零

📅  最后修改于: 2021-04-24 15:35:00             🧑  作者: Mango

给定整数n,编写一个函数,返回n!中尾随零的计数。
例子 :

Input: n = 5
Output: 1 
Factorial of 5 is 120 which has one trailing 0.

Input: n = 20
Output: 4
Factorial of 20 is 2432902008176640000 which has
4 trailing zeroes.

Input: n = 100
Output: 24

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

  1. 方法:
    一种简单的方法是首先计算n的阶乘,然后计算结果中的尾随0(我们可以通过重复将阶乘除以10直到余数为0来计数尾随0。)
  2. 由于一个数字的阶乘是一个大数字,上述方法可能会导致稍大的数字发生溢出(请参见上面示例中给出的20的阶乘)。这个想法是考虑阶乘n的素因子。尾随零总是由素因子2和5产生。如果我们可以算出5s和2s的数量,那么我们的任务就完成了。考虑以下示例。
    n = 5: 5的素因子中有1个5和3 2s! (2 * 2 * 2 * 3 * 5)。因此尾随0的计数为1。
    n = 11:11的素因子有2个5s和8个2s! (2 8 * 3 4 * 5 2 * 7)。因此尾随0的计数为2。
  3. 我们可以很容易地观察到,素数中2s的数量始终大于或等于5s的数量。因此,如果我们将5s计为主要因素,我们就完成了。如何计算n!的素数中5s总数?一种简单的方法是计算下限(n / 5)。例如7!有一个5、10!有两个5s。还没有完成,还有一件事要考虑。诸如25、125等数字具有多个1。例如,如果我们考虑28!我们得到一个额外的5,0的个数变为6。处理起来很简单,首先将n除以5并去除所有单个5s,然后除以25以去除多余的5s,依此类推。以下是计算尾随0的汇总公式。
Trailing 0s in n! = Count of 5s in prime factors of n!
                  = floor(n/5) + floor(n/25) + floor(n/125) + ....

以下是基于上述公式的程序:

C++
// C++ program to count
// trailing 0s in n!
#include 
using namespace std;
 
// Function to return trailing
// 0s in factorial of n
int findTrailingZeros(int n)
{
    // Initialize result
    int count = 0;
 
    // Keep dividing n by powers of
    // 5 and update count
    for (int i = 5; n / i >= 1; i *= 5)
        count += n / i;
 
    return count;
}
 
// Driver Code
int main()
{
    int n = 100;
    cout << "Count of trailing 0s in " << 100
         << "! is " << findTrailingZeros(n);
    return 0;
}


Java
// Java program to count
// trailing 0s in n!
import java.io.*;
 
class GFG
{
    // Function to return trailing
    // 0s in factorial of n
    static int findTrailingZeros(int n)
    {
        // Initialize result
        int count = 0;
 
        // Keep dividing n by powers
        // of 5 and update count
        for (int i = 5; n / i >= 1; i *= 5)
            count += n / i;
 
        return count;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 100;
        System.out.println("Count of trailing 0s in " +
                                           n +"! is " +
                                 findTrailingZeros(n));
    }
}
 
// This code is contributed by Pramod Kumar


Python3
# Python3 program to
# count trailing 0s
# in n!
 
# Function to return
# trailing 0s in
# factorial of n
 
 
def findTrailingZeros(n):
 
    # Initialize result
    count = 0
 
    # Keep dividing n by
    # 5 & update Count
    while(n >= 5):
        n //= 5
        count += n
 
    return count
 
 
# Driver program
n = 100
print("Count of trailing 0s " +
      "in 100! is", findTrailingZeros(n))
 
# This code is contributed by Uttam Singh


C#
// C# program to count
// trailing 0s in n!
using System;
 
class GFG
{
     
    // Function to return trailing
    // 0s in factorial of n
    static int findTrailingZeros(int n)
    {
        // Initialize result
        int count = 0;
 
        // Keep dividing n by powers
        // of 5 and update count
        for (int i = 5; n / i >= 1; i *= 5)
            count += n / i;
 
        return count;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 100;
        Console.WriteLine("Count of trailing 0s in " +
                                           n +"! is "+
                                findTrailingZeros(n));
    }
}
 
// This code is contributed by vt_m


PHP
= 1; $i *= 5)
        $count += $n / $i;
 
    return $count;
}
 
// Driver Code
 
$n = 100;
echo "Count of trailing 0s in " , 100,
     "! is " , findTrailingZeros($n);
 
// This code is contributed by vt_m
?>


Javascript


输出 :

Count of trailing 0s in 100! is 24