📌  相关文章
📜  阶乘中至少n个尾随零的最小数字

📅  最后修改于: 2021-04-29 06:24:27             🧑  作者: Mango

给定数字n 。任务是找到其阶乘包含至少n个尾随零的最小数字。
例子 :

Input : n = 1
Output : 5 
1!, 2!, 3!, 4! does not contain trailing zero.
5! = 120, which contains one trailing zero.

Input : n = 6
Output : 25

在关于数的阶乘计数尾随零的文章中,我们讨论了零的个数等于x!的质数中的5的个数。我们已经讨论了以下公式来计算5的数量。

Trailing 0s in x! = Count of 5s in prime factors of x!
                  = floor(x/5) + floor(x/25) + floor(x/125) + ....

让我们举几个例子来观察模式

5!  has 1 trailing zeroes 
[All numbers from 6 to 9
 have 1 trailing zero]

10! has 2 trailing zeroes
[All numbers from 11 to 14
 have 2 trailing zeroes]

15! to 19! have 3 trailing zeroes

20! to 24! have 4 trailing zeroes

25! to 29! have 6 trailing zeroes

我们可以注意到,阶乘包含n个尾随零的最小值为5 * n。
因此,要查找其阶乘包含n个尾随零的最小值,请在0到5 * n的范围内使用二进制搜索。并且,找到其阶乘包含n个尾随零的最小数字。

C++
// C++ program tofind smallest number whose
// factorial contains at least n trailing
// zeroes.
#include
using namespace std;
 
// Return true if number's factorial contains
// at least n trailing zero else false.
bool check(int p, int n)
{
    int temp = p, count = 0, f = 5;
    while (f <= temp)
    {
        count += temp/f;
        f = f*5;
    }
    return (count >= n);
}
 
// Return smallest number whose factorial
// contains at least n trailing zeroes
int findNum(int n)
{
    // If n equal to 1, return 5.
    // since 5! = 120.
    if (n==1)
        return 5;
 
    // Initalising low and high for binary
    // search.
    int low = 0;
    int high = 5*n;
 
    // Binary Search.
    while (low > 1;
 
        // Checking if mid's factorial contains
        // n trailing zeroes.
        if (check(mid, n))
            high = mid;
        else
            low = mid+1;
    }
 
    return low;
}
 
// driver code
int main()
{
    int n = 6;
    cout << findNum(n) << endl;
    return 0;
}


Java
// Java program tofind smallest number whose
// factorial contains at least n trailing
// zeroes.
 
class GFG
{
    // Return true if number's factorial contains
    // at least n trailing zero else false.
    static boolean check(int p, int n)
    {
        int temp = p, count = 0, f = 5;
        while (f <= temp)
        {
            count += temp / f;
            f = f * 5;
        }
        return (count >= n);
    }
     
    // Return smallest number whose factorial
    // contains at least n trailing zeroes
    static int findNum(int n)
    {
        // If n equal to 1, return 5.
        // since 5! = 120.
        if (n==1)
            return 5;
     
        // Initalising low and high for binary
        // search.
        int low = 0;
        int high = 5 * n;
     
        // Binary Search.
        while (low < high)
        {
            int mid = (low + high) >> 1;
     
            // Checking if mid's factorial
            // contains n trailing zeroes.
            if (check(mid, n))
                high = mid;
            else
                low = mid + 1;
        }
     
        return low;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 6;
        System.out.println(findNum(n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program tofind smallest
# number whose
# factorial contains at least
# n trailing zeroes
 
# Return true if number's factorial contains
# at least n trailing zero else false.
def check(p,n):
 
    temp = p
    count = 0
    f = 5
    while (f <= temp):
        count += temp/f
        f = f*5
 
    return (count >= n)
 
# Return smallest number whose factorial
# contains at least n trailing zeroes
def findNum(n):
 
    # If n equal to 1, return 5.
    # since 5! = 120.
    if (n==1):
        return 5
  
    # Initalizing low and high for binary
    # search.
    low = 0
    high = 5*n
  
    # Binary Search.
    while (low > 1
  
        # Checking if mid's factorial contains
        # n trailing zeroes.
        if (check(mid, n)):
            high = mid
        else:
            low = mid+1
     
  
    return low
 
 
# driver code
n = 6
print(findNum(n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program tofind smallest number whose
// factorial contains at least n trailing
// zeroes.
using System;
 
class GFG
{
    // Return true if number's factorial contains
    // at least n trailing zero else false.
    static bool check(int p, int n)
    {
        int temp = p, count = 0, f = 5;
        while (f <= temp)
        {
            count += temp / f;
            f = f * 5;
        }
        return (count >= n);
    }
     
    // Return smallest number whose factorial
    // contains at least n trailing zeroes
    static int findNum(int n)
    {
        // If n equal to 1, return 5.
        // since 5! = 120.
        if (n == 1)
            return 5;
     
        // Initalising low and high for binary
        // search.
        int low = 0;
        int high = 5 * n;
     
        // Binary Search.
        while (low < high)
        {
            int mid = (low + high) >> 1;
     
            // Checking if mid's factorial
            // contains n trailing zeroes.
            if (check(mid, n))
                high = mid;
            else
                low = mid + 1;
        }
     
        return low;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 6;
         
        Console.WriteLine(findNum(n));
    }
}
 
// This code is contributed by vt_m.


PHP
= $n);
}
 
// Return smallest number
// whose factorial contains
// at least n trailing zeroes
function findNum($n)
{
    // If n equal to 1, return 5.
    // since 5! = 120.
    if ($n == 1)
        return 5;
 
    // Initalising low and high
    // for binary search.
    $low = 0;
    $high = 5 * $n;
 
    // Binary Search.
    while ($low < $high)
    {
        $mid = ($low + $high) >> 1;
 
        // Checking if mid's factorial
        // contains n trailing zeroes.
        if (check($mid, $n))
            $high = $mid;
        else
            $low = $mid + 1;
    }
 
    return $low;
}
 
// Driver Code
$n = 6;
echo(findNum($n));
 
// This code is contributed by Ajit.
?>


Javascript


输出 :

25