📜  N阶乘以N的位数

📅  最后修改于: 2021-06-26 21:16:56             🧑  作者: Mango

给定一个正整数N,我们必须找到N的阶乘中被提高到幂N的位数,即(N!)^N
例子:

输入:4输出:6说明: (4!)^4 = (24)^4 =331776。331776中的总位数为6。输入:5输出:11说明: (5!)^5 = (120)^5 = 24883200000 24883200000中的总位数为11。输入:2输出:1输入:1000输出:2567605

解决方案的想法如下所述。

下面给出了上述想法的代码实现。

C++
// CPP program to find count of digits in N
// factorial raised to N
#include 
using namespace std;
 
int countDigits(int n)
{
    // we take sum of logarithms as explained
    // in the approach
    double ans = 0;
    for (int i = 1; i <= n; i++)
        ans += log10(i);
 
    // multiply the result with n
    ans = ans * n;
    return 1 + floor(ans);
}
 
int main()
{
    int n = 4;
    cout << countDigits(n) << "\n";
    return 0;
}


Java
// Java program to find
// count of digits in N
// factorial raised to N
import java.io.*;
 
class GFG
{
static int countDigits(int n)
{
    // we take sum of logarithms
    // as explained in the approach
    double ans = 0;
    for (int i = 1; i <= n; i++)
        ans += Math.log10(i);
 
    // multiply the
    // result with n
    ans = ans * n;
    return 1 + (int)Math.floor(ans);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
    System.out.println(
               countDigits(n) + "\n");
}
}
 
// This code is contributed
// by anuj_67.


Python3
# python3 program to find count of digits in N
# factorial raised to N
 
import math
  
def countDigits( n):
 
    # we take sum of logarithms as explained
    # in the approach
    ans = 0
    for i in range (1,n+1):
        ans += math.log10(i)
  
    #multiply the result with n
    ans = ans * n
    return 1 + math.floor(ans)
 
  
if __name__ == "__main__":
 
    n = 4
    print (countDigits(n))


C#
// C# program to find
// count of digits in N
// factorial raised to N
using System;
 
class GFG
{
static int countDigits(int n)
{
    // we take sum of logarithms
    // as explained in the approach
    double ans = 0;
    for (int i = 1; i <= n; i++)
        ans += Math.Log10(i);
 
    // multiply the
    // result with n
    ans = ans * n;
    return 1 + (int)Math.Floor(ans);
}
 
// Driver Code
public static void Main ()
{
    int n = 4;
    Console.WriteLine(
            countDigits(n) + "\n");
}
}
 
// This code is contributed
// by anuj_67.


PHP


Javascript


输出:
6

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