Find the total Number of Digits in (N!)N
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++
// 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!)^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; } |
chevron_right
filter_none
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) |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
<?php // PHP 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; $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 ?> |
chevron_right
filter_none
Output:
11
Recommended Posts:
- Count total number of digits from 1 to n
- Total number of non-decreasing numbers with n digits
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find the total number of composite factor for a given number
- Find the Largest number with given number of digits and sum of digits
- Find out the minimum number of coins required to pay total amount
- Program to find total number of edges in a Complete Graph
- Total numbers with no repeated digits in a range
- Find the smallest number whose digits multiply to a given number n
- Find count of digits in a number that divide the number
- Find maximum number that can be formed using digits of a given number
- Given a number n, find the first k digits of n^n
- Find first and last digits of a number
- Find M-th number whose repeated sum of digits of a number is N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



