Count digits in a factorial | Set 1 - GeeksforGeeks

03 Mar, 2021
Given an integer n, find the number of digits that appear in its factorial, where factorial is defined as, factorial(n) = 1*2*3*4........*n and factorial(0) = 1Examples :  
Input :  n = 1
Output : 1
1! = 1 , hence number of digits is 1

Input :  5
Output : 3
5! = 120, i.e., 3 digits

Input : 10
Output : 7
10! = 3628800, i.e., 7 digits
 
A naive solution would be to calculate the n! first and then calculate the number of digits present in it. However as the value for n! can be very large, it would become cumbersome to store them in a variable (Unless you’re working in python!) . A better solution would be to use the useful property of logarithms to calculate the required answer.  
We know,
log(a*b) = log(a) + log(b)

Therefore
log( n! ) = log(1*2*3....... * n) 
          = log(1) + log(2) + ........ +log(n)

Now, observe that the floor value of log base 
10 increased by 1, of any number, gives the
number of digits present in that number.

Hence, output would be : floor(log(n!)) + 1.
Below is a the implementation of the same.  
C++
Java
Python3
C#
PHP
Javascript
// A C++ program to find the number of digits in// a factorial#include <bits/stdc++.h>using namespace std; // This function receives an integer n, and returns// the number of digits present in n!int findDigits(int n){    // factorial exists only for n>=0    if (n < 0)        return 0;     // base case    if (n <= 1)        return 1;     // else iterate through n and calculate the    // value    double digits = 0;    for (int i=2; i<=n; i++)        digits += log10(i);     return floor(digits) + 1;} // Driver codeint main(){    cout << findDigits(1) << endl;    cout << findDigits(5) << endl;    cout << findDigits(10) << endl;    cout << findDigits(120) << endl;    return 0;}
// Java program to find the number// of digits in a factorial import java.io.*;import java.util.*; class GFG{    // returns the number of digits    // present in n!    static int findDigits(int n)    {        // factorial exists only for n>=0        if (n < 0)            return 0;          // base case        if (n <= 1)            return 1;          // else iterate through n and calculate the        // value        double digits = 0;        for (int i=2; i<=n; i++)            digits += Math.log10(i);          return (int)(Math.floor(digits)) + 1;    }         // Driver code    public static void main (String[] args)    {        System.out.println(findDigits(1));        System.out.println(findDigits(5));        System.out.println(findDigits(10));        System.out.println(findDigits(120));    }} // This code is contributed by Pramod Kumar
# Python3 program to find the# number of digits in a factorialimport math # This function receives an integer# n, and returns the number of# digits present in n! def findDigits(n):         # factorial exists only for n>=0    if (n < 0):        return 0;     # base case    if (n <= 1):        return 1;     # else iterate through n and    # calculate the value    digits = 0;    for i in range(2, n + 1):        digits += math.log10(i);     return math.floor(digits) + 1; # Driver codeprint(findDigits(1));print(findDigits(5));print(findDigits(10));print(findDigits(120)); # This code is contributed by mits
// A C++ program to find the number// of digits in a factorialusing System; class GFG {         // This function receives an integer    // n, and returns the number of    // digits present in n!    static int findDigits(int n)    {                 // factorial exists only for n>=0        if (n < 0)            return 0;             // base case        if (n <= 1)            return 1;             // else iterate through n and        // calculate the value        double digits = 0;        for (int i = 2; i <= n; i++)            digits += Math.Log10(i);             return (int)Math.Floor(digits) + 1;    }         // Driver code    public static void Main()    {        Console.Write(findDigits(1) + "\n");        Console.Write(findDigits(5) + "\n");        Console.Write(findDigits(10) + "\n");        Console.Write(findDigits(120) + "\n");    }} // This code is contributed by// Smitha Dinesh Semwal
<?php// PHP program to find// the number of digits// in a factorial // This function receives// an integer n, and returns// the number of digits present in n! function findDigits($n){    // factorial exists only for n>=0    if ($n < 0)        return 0;     // base case    if ($n <= 1)        return 1;     // else iterate through n and    // calculate the value    $digits = 0;    for ($i = 2; $i <= $n; $i++)        $digits += log10($i);     return floor($digits) + 1;} // Driver codeecho findDigits(1), "\n";echo findDigits(5), "\n";echo findDigits(10), "\n";echo findDigits(120), "\n"; // This code is contributed by Ajit.?>
<script> // A Javascript program to find the number of digits in// a factorial  // This function receives an integer n, and returns// the number of digits present in n!function findDigits(n){    // factorial exists only for n>=0    if (n < 0)        return 0;     // base case    if (n <= 1)        return 1;     // else iterate through n and calculate the    // value    let digits = 0;    for (let i=2; i<=n; i++)        digits += Math.log10(i);     return Math.floor(digits) + 1;} // Driver code      document.write(findDigits(1) + "<br>");    document.write(findDigits(5) + "<br>");    document.write(findDigits(10) + "<br>");    document.write(findDigits(120) + "<br>");      //This code is contributed by Mayank Tyagi </script>
Output : 
1
3
7
199
In the next set, we’d see how to further optimize our approach and reduce the time complexity for the same program.This article is contributed by Ashutosh Kumar .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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Smitha Dinesh Semwal
jit_t
Mithun Kumar
mayanktyagi1709
factorial
number-digits
Mathematical
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)
Program for Decimal to Binary Conversion
Algorithm to solve Rubik's Cube
Minimum number of jumps to reach end