📜  两个阶乘积的尾随0

📅  最后修改于: 2021-04-23 19:36:57             🧑  作者: Mango

给定两个整数N或M,可以找到阶乘积(N!* M!)的零尾数?
例子:

Input : N = 4, M = 5 
Output :  1
Explanation : 4! = 24, 5! = 120
Product has only 1 trailing 0.

Input : N = 127!, M = 57!
Output : 44

正如在N中的零数目所讨论的!可以通过将N递归除以5并乘以商来计算。

N中的0数! =31。类似地,对于M我们可以计算并相加。
因此,通过以上我们可以得出N!* M!中的零个数。等于N中的零个数之和!和M!

C++
// CPP program for count number of trailing zeros
// in N! * M!
#include 
using namespace std;
 
// Returns number of zeros in factorial n
int trailingZero(int x)
{
    // dividing x by powers of 5 and update count
    int i = 5, count = 0;
    while (x > i) {
        count = count + x / i;
        i = i * 5;
    }
    return count;
}
 
// Returns count of trailing zeros in M! x N!
int countProductTrailing(int M, int N)
{
   return trailingZero(N) + trailingZero(M);
}
 
// Driver program
int main()
{
    int N = 67, M = 98;
    cout << countProductTrailing(N, M);
    return 0;
}


Java
// Java program for count number
// of trailing zeros in N! * M!
import java.io.*;
 
class GFG {
     
    // Returns number of zeros in factorial n
    static int trailingZero(int x)
    {
        // dividing x by powers
        // of 5 and update count
        int i = 5, count = 0;
         
        while (x > i) {
             
            count = count + x / i;
            i = i * 5;
        }
        return count;
    }
     
    // Returns count of trailing zeros in M! x N!
    static int countProductTrailing(int M, int N)
    {
    return trailingZero(N) + trailingZero(M);
    }
     
    // Driver program
    public static void main(String args[])
    {
        int N = 67, M = 98;
        System.out.println(countProductTrailing(N, M));
    }
}
 
 
/* This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program for count
# number of trailing zeros
# in N ! * M !
 
# Returns number of zeros in
# factorial n
def trailingZero(x) :
 
    # Dividing x by powers of 5
    # and update count
    i = 5
    count = 0
     
    while (x > i) :
        count = count + x // i
        i = i * 5
     
    return count
     
# Returns count of trailing
# zeros in M ! x N !
def countProductTrailing(M, N) :
    return trailingZero(N) + trailingZero(M)
     
# Driver program
N = 67
M = 98
print(countProductTrailing(N, M))
 
# This code is contributed by Nikita Tiwari.


C#
// Java program for count number
// of trailing zeros in N! * M!
using System;
 
class GFG {
     
    // Returns number of zeros in factorial n
    static int trailingZero(int x)
    {
        // dividing x by powers
        // of 5 and update count
        int i = 5, count = 0;
         
        while (x > i) {
             
            count = count + x / i;
            i = i * 5;
        }
        return count;
    }
     
    // Returns count of trailing zeros in M! x N!
    static int countProductTrailing(int M, int N)
    {
    return trailingZero(N) + trailingZero(M);
    }
     
    // Driver program
    public static void Main()
    {
        int N = 67, M = 98;
        Console.WriteLine(countProductTrailing(N, M));
    }
}
 
 
/* This code is contributed by vt_m.*/


PHP
 $i)
    {
        $count = $count + (int)($x / $i);
        $i = $i * 5;
    }
    return $count;
}
 
// Returns count of trailing
// zeros in M! x N!
function countProductTrailing($M, $N)
{
    return trailingZero($N) + trailingZero($M);
}
 
// Driver Code
$N = 67; $M = 98;
echo(countProductTrailing($N, $M));
 
// This code is contributed by Ajit.
?>


Javascript


输出:

37