📜  计算阶乘中的数字|套装1

📅  最后修改于: 2021-04-29 09:32:03             🧑  作者: Mango

给定一个整数n,找到在其阶乘中出现的位数,其中阶乘被定义为:factorial(n)= 1 * 2 * 3 * 4…….. * n和factorial(0)= 1
例子 :

Input :  n = 1
Output : 1
1! = 1 , hence number of digits is 1

Input :  5
Output : 3
5! = 120, i.e., 3 digits

Input : 10
Output : 7
10! = 3628800, i.e., 7 digits

一个简单的解决方案是计算n!首先,然后计算其中的数字位数。但是作为n的值!可能非常大,将它们存储在变量中将变得很麻烦(除非您使用的是Python!)。
更好的解决方案是使用对数的有用属性来计算所需的答案。

We know,
log(a*b) = log(a) + log(b)

Therefore
log( n! ) = log(1*2*3....... * n) 
          = log(1) + log(2) + ........ +log(n)

Now, observe that the floor value of log base 
10 increased by 1, of any number, gives the
number of digits present in that number.

Hence, output would be : floor(log(n!)) + 1.

下面是一个相同的实现。

C++
// A C++ program to find the number of digits in
// a factorial
#include 
using namespace std;
 
// This function receives an integer n, and returns
// the number of digits present in n!
int findDigits(int n)
{
    // factorial exists only for n>=0
    if (n < 0)
        return 0;
 
    // base case
    if (n <= 1)
        return 1;
 
    // else iterate through n and calculate the
    // value
    double digits = 0;
    for (int i=2; i<=n; i++)
        digits += log10(i);
 
    return floor(digits) + 1;
}
 
// Driver code
int main()
{
    cout << findDigits(1) << endl;
    cout << findDigits(5) << endl;
    cout << findDigits(10) << endl;
    cout << findDigits(120) << endl;
    return 0;
}


Java
// Java program to find the number
// of digits in a factorial
 
import java.io.*;
import java.util.*;
 
class GFG
{
    // returns the number of digits
    // present in n!
    static int findDigits(int n)
    {
        // factorial exists only for n>=0
        if (n < 0)
            return 0;
  
        // base case
        if (n <= 1)
            return 1;
  
        // else iterate through n and calculate the
        // value
        double digits = 0;
        for (int i=2; i<=n; i++)
            digits += Math.log10(i);
  
        return (int)(Math.floor(digits)) + 1;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        System.out.println(findDigits(1));
        System.out.println(findDigits(5));
        System.out.println(findDigits(10));
        System.out.println(findDigits(120));
    }
}
 
// This code is contributed by Pramod Kumar


Python3
# Python3 program to find the
# number of digits in a factorial
import math
 
# This function receives an integer
# n, and returns the number of
# digits present in n!
 
def findDigits(n):
     
    # factorial exists only for n>=0
    if (n < 0):
        return 0;
 
    # base case
    if (n <= 1):
        return 1;
 
    # else iterate through n and
    # calculate the value
    digits = 0;
    for i in range(2, n + 1):
        digits += math.log10(i);
 
    return math.floor(digits) + 1;
 
# Driver code
print(findDigits(1));
print(findDigits(5));
print(findDigits(10));
print(findDigits(120));
 
# This code is contributed by mits


C#
// A C++ program to find the number
// of digits in a factorial
using System;
 
class GFG {
     
    // This function receives an integer
    // n, and returns the number of
    // digits present in n!
    static int findDigits(int n)
    {
         
        // factorial exists only for n>=0
        if (n < 0)
            return 0;
     
        // base case
        if (n <= 1)
            return 1;
     
        // else iterate through n and
        // calculate the value
        double digits = 0;
        for (int i = 2; i <= n; i++)
            digits += Math.Log10(i);
     
        return (int)Math.Floor(digits) + 1;
    }
     
    // Driver code
    public static void Main()
    {
        Console.Write(findDigits(1) + "\n");
        Console.Write(findDigits(5) + "\n");
        Console.Write(findDigits(10) + "\n");
        Console.Write(findDigits(120) + "\n");
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


PHP
=0
    if ($n < 0)
        return 0;
 
    // base case
    if ($n <= 1)
        return 1;
 
    // else iterate through n and
    // calculate the value
    $digits = 0;
    for ($i = 2; $i <= $n; $i++)
        $digits += log10($i);
 
    return floor($digits) + 1;
}
 
// Driver code
echo findDigits(1), "\n";
echo findDigits(5), "\n";
echo findDigits(10), "\n";
echo findDigits(120), "\n";
 
// This code is contributed by Ajit.
?>


Javascript


输出 :

1
3
7
199