Find the total Number of Digits in (N!)N

30 Apr, 2021
Given a number N. The task is to find the total Number of Digits in .Examples:  
Input: N = 3
Output: 3
If N=3, (3!)3=216, 
So the count of digits is 3

Input: N = 4
Output: 6
 
Approach:  
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)
Now, we know that the floor value of log base 10 increased by 1, of any number, gives the number of digits present in that number. That is, number of digits in a number say N will be floor(log10N) + 1.Therefore, number of digit in will be:  
floor(log())+1
= floor(N*log10(N!)) + 1
= floor(N*X) + 1.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the total// Number of Digits in (N!)^N #include <bits/stdc++.h>using namespace std; // Function to find the total// Number of Digits in (N!)^Nint 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 codeint main(){    int N = 5;     cout << CountDigits(N);     return 0;}
// Java program to find the total// Number of Digits in (N!)^Nimport java.io.*;import java.util.*;import java.lang.*; class GFG{// Function to find the total// Number of Digits in (N!)^Npublic 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 codepublic 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 program to find the total# Number of Digits in (N!)^N import math as madef 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 codeif __name__=='__main__':    N=5    print(CountDigits(N)) # This code is contributed by# Indrajit Sinha.
// C# program to find the total// Number of Digits in (N!)^Nusing System; class GFG{// Function to find the total// Number of Digits in (N!)^Npublic 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 codepublic static void Main(){    GFG g = new GFG();    int N = 5;    Console.WriteLine(g.CountDigits(N));}} // This code is contributed// by SoumikMondal
<?php// PHP program to find the total// Number of Digits in (N!)^N // Function to find the total// Number of Digits in (N!)^Nfunction CountDigits($n){    if ($n == 1)        return 1;     $sum = 0;     // Finding X    for ($i = 2; $i <= $n; ++$i)    {        $sum += log($i) / log(10);    }     // Calculating N*X    $sum *= $n;     // Floor(N*X) + 1    return ceil($sum); // equivalent to floor(sum) + 1} // Driver code$N = 5;echo CountDigits($N); // This code is contributed by ajit?>
<script>// javascript program to find the total// Number of Digits in (N!)^N    // Function to find the total    // Number of Digits in (N!)^N    function CountDigits(n) {        if (n == 1)            return 1;         var sum = 0;         // Finding X        for (i = 2; i <= n; ++i) {            sum += (Math.log(i) /  Math.log(10));        }         // Calculating N*X        sum *= n;         // Floor(N*X) + 1        // equivalent to floor(sum) + 1        return Math.ceil(sum);    }     // Driver code             var N = 5;        document.write(CountDigits(N)); // This code contributed by aashish1995</script>
11
 
SoumikMondal
Akanksha_Rai
jit_t
indrajit1
aashish1995
factorial
maths-log
school-programming
C++
Mathematical
Mathematical
factorial
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Writing First C++ Program - Hello World Example
Basic Input / Output in C++
Functions that cannot be overloaded in C++
Switch Statement in C/C++
Polymorphism in C++
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Operators in C / C++
Merge two sorted arrays
The Knight's tour problem | Backtracking-1