📜  在(N!)N中查找位数

📅  最后修改于: 2021-05-04 22:41:52             🧑  作者: Mango

给定数字N。任务是找到总位数 (N!)^{N}

例子

Input: N = 3
Output: 3
If N=3, (3!)3=216, 
So the count of digits is 3

Input: N = 4
Output: 6

方法:

As we know,
log(a*b) = log(a) + log(b)

Consider,
X = log(N!) = log(1*2*3....... * N) 
            = log(1)+log(2)+........ +log(N)

现在,我们知道以10为底的对数底数增加了1,无论任何数字,都会给出该数字中存在的位数。也就是说,数字中说N的位数将是floor(log 10 N)+1

因此,输入的位数(N!)^{N}将:

地板(log( (N!)^{N} ))+ 1 =下限(N * log 10 (N!))+ 1 =下限(N * X)+ 1。

下面是上述方法的实现:

C++
// C++ program to find the total
// Number of Digits in (N!)^N
  
#include 
using namespace std;
  
// Function to find the total
// Number of Digits in (N!)^N
int CountDigits(int n)
{
    if (n == 1)
        return 1;
  
    double sum = 0;
  
    // Finding X
    for (int i = 2; i <= n; ++i) {
        sum += (double)log(i) / (double)log(10);
    }
  
    // Calculating N*X
    sum *= (double)n;
  
    // Floor(N*X) + 1
    return ceil(sum); // equivalent to floor(sum) + 1
}
  
// Driver code
int main()
{
    int N = 5;
  
    cout << CountDigits(N);
  
    return 0;
}


Java
// Java program to find the total 
// Number of Digits in (N!)^N 
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG
{
// Function to find the total 
// Number of Digits in (N!)^N 
public double CountDigits(int n) 
{ 
    if (n == 1) 
        return 1; 
  
    double sum = 0; 
  
    // Finding X 
    for (int i = 2; i <= n; ++i)
    { 
        sum += ((double)Math.log(i) / 
                (double)Math.log(10)); 
    } 
  
    // Calculating N*X 
    sum *= n; 
  
    // Floor(N*X) + 1 
    // equivalent to floor(sum) + 1 
    return Math.ceil(sum); 
} 
  
// Driver code 
public static void main(String args[]) 
{ 
    GFG g = new GFG();
    int N = 5; 
    System.out.println(g.CountDigits(N)); 
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)


Python3
# Python3 program to find the total 
# Number of Digits in (N!)^N
  
import math as ma
def CountDigits(n):
  
    if(n==1):
        return 1
    sum=0
  
    # Finding X
    for i in range(2,n+1):
        sum+=ma.log(i,10)
  
    # Calculating N*X 
    sum*=n
  
    # Floor(N*X)+1
    #equivalent to floor(sum) + 1
    return ma.ceil(sum) 
  
# Driver code
if __name__=='__main__':
    N=5
    print(CountDigits(N))
  
# This code is contributed by 
# Indrajit Sinha.


C#
// C# program to find the total 
// Number of Digits in (N!)^N 
using System;
  
class GFG
{
// Function to find the total 
// Number of Digits in (N!)^N 
public double CountDigits(int n) 
{ 
    if (n == 1) 
        return 1; 
  
    double sum = 0; 
  
    // Finding X 
    for (int i = 2; i <= n; ++i)
    { 
        sum += ((double)Math.Log(i) / 
                (double)Math.Log(10)); 
    } 
  
    // Calculating N*X 
    sum *= n; 
  
    // Floor(N*X) + 1 
    // equivalent to floor(sum) + 1 
    return Math.Ceiling(sum); 
} 
  
// Driver code 
public static void Main() 
{ 
    GFG g = new GFG();
    int N = 5; 
    Console.WriteLine(g.CountDigits(N)); 
}
}
  
// This code is contributed 
// by SoumikMondal


PHP


输出:
11