# Number of digits in N factorial to the power N

Given a positive integer N, we have to find the total number of digits in the factorial of N raised to the power N, i.e,

**Examples:**

Input: 4 Output: 6 Explanations: = = 331776. Total number of digits in 331776 is 6. Input: 5 Output: 11 Explanations: = = 24883200000 Total number of digits in 24883200000 is 11. Input: 2 Output: 1 Input: 1000 Output: 2567605

The idea of the solution is described below.

Brute force method:By brute force, we can simply compute N! in O(N) time and then multiply it n times but that will be a very slow method and would exceed both time and space because would be a huge number.

Efficient method:

Let’s look at it more closely. We can break (N!)^N into simpler terms which are easy to compute. By taking common logarithm,

we get

and we know,

Therefore we can reduce further.

Now we can compute the answer easily in O(N) time and without space limit exceeding.

So why is a valid answer to the problem?

We know that the total number of digits in a number N whose base is 10 can be easily found by taking ceil(log10 (N)). That is exactly done in the approach described above.

The code implementation of the above idea is given below.

## C++

`// CPP program to find count of digits in N ` `// factorial raised to N ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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)) ` ` ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find count of ` `// digits in N factorial raised to N ` `function` `countDigits (` `$n` `) ` `{ ` ` ` `// we take sum of logarithms as ` ` ` `// explained in the approach ` ` ` `$ans` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$ans` `+= log10(` `$i` `); ` ` ` ` ` `// multiply the result with n ` ` ` `$ans` `= ` `$ans` `* ` `$n` `; ` ` ` `return` `1 + ` `floor` `(` `$ans` `); ` `} ` ` ` `// Driver Code ` `$n` `= 4; ` `echo` `countDigits(` `$n` `) , ` `"\n"` `; ` ` ` `// This code is contributed ` `// by jit_t ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Find maximum power of a number that divides a factorial
- Smallest number with at least n digits in factorial
- Find sum of digits in factorial of a number
- Sum of digits of a given number to a given power
- Number of digits in 2 raised to power n
- Find last five digits of a given five digit number raised to power five
- Largest power of k in n! (factorial) where k may not be prime
- Count digits in a factorial | Set 1
- Count digits in a factorial | Set 2
- Print last k digits of a^b (a raised to power b)
- Check if given number is a power of d where d is a power of 2
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M

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.