Count digits in a factorial using Logarithm
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) = 1
Examples :
Input: 5
Output: 3
Explanation: 5! = 120, i.e., 3 digits
Input: 10
Output: 7
Explanation: 10! = 3628800, i.e., 7 digits
Naive approach: To solve the problem follow the below idea:
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!).
Count digits in a factorial using the property of logarithms:
To solve the problem follow the below idea:
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 the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findDigits( int n)
{
if (n < 0)
return 0;
if (n <= 1)
return 1;
double digits = 0;
for ( int i = 2; i <= n; i++)
digits += log10 (i);
return floor (digits) + 1;
}
int main()
{
cout << findDigits(1) << endl;
cout << findDigits(5) << endl;
cout << findDigits(10) << endl;
cout << findDigits(120) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findDigits( int n)
{
if (n < 0 )
return 0 ;
if (n <= 1 )
return 1 ;
double digits = 0 ;
for ( int i = 2 ; i <= n; i++)
digits += Math.log10(i);
return ( int )(Math.floor(digits)) + 1 ;
}
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 ));
}
}
|
Python3
import math
def findDigits(n):
if (n < 0 ):
return 0
if (n < = 1 ):
return 1
digits = 0
for i in range ( 2 , n + 1 ):
digits + = math.log10(i)
return math.floor(digits) + 1
if __name__ = = "__main__" :
print (findDigits( 1 ))
print (findDigits( 5 ))
print (findDigits( 10 ))
print (findDigits( 120 ))
|
Javascript
function findDigits(n)
{
if (n < 0)
return 0;
if (n <= 1)
return 1;
let digits = 0;
for (let i=2; i<=n; i++)
digits += Math.log10(i);
return Math.floor(digits) + 1;
}
document.write(findDigits(1) + "<br>" );
document.write(findDigits(5) + "<br>" );
document.write(findDigits(10) + "<br>" );
document.write(findDigits(120) + "<br>" );
|
C#
using System;
class GFG {
static int findDigits( int n)
{
if (n < 0)
return 0;
if (n <= 1)
return 1;
double digits = 0;
for ( int i = 2; i <= n; i++)
digits += Math.Log10(i);
return ( int )Math.Floor(digits) + 1;
}
public static void Main()
{
Console.Write(findDigits(1) + "\n" );
Console.Write(findDigits(5) + "\n" );
Console.Write(findDigits(10) + "\n" );
Console.Write(findDigits(120) + "\n" );
}
}
|
PHP
<?php
function findDigits( $n )
{
if ( $n < 0)
return 0;
if ( $n <= 1)
return 1;
$digits = 0;
for ( $i = 2; $i <= $n ; $i ++)
$digits += log10( $i );
return floor ( $digits ) + 1;
}
echo findDigits(1), "\n" ;
echo findDigits(5), "\n" ;
echo findDigits(10), "\n" ;
echo findDigits(120), "\n" ;
?>
|
Time complexity: O(N log N) since calculating log in a loop
Auxiliary space: O(1) because it is using constant variables
Approach 2: Using Stirling’s approximation formula to calculate the factorial and logarithm to count the number of digits.
- The countDigitsInFactorial(int n) function takes an integer n as input and returns the number of digits in the factorial of n. If n is negative, it returns 0. If n is 0 or 1, the factorial is 1, and it returns 1.
- In the countDigitsInFactorial(int n) function, the double x variable is declared and initialized using the Stirling’s approximation formula for the factorial. This formula provides a good approximation of the value of the factorial for large values of n.
- where e is the mathematical constant, and ? is the mathematical constant pi.
- The formula used in this code is a simplified version of Stirling’s approximation that takes the logarithm of the above formula to get the number of digits in the factorial.
C++
#include <cmath>
#include <iostream>
using namespace std;
int countDigitsInFactorial( int n)
{
if (n < 0) {
return 0;
}
if (n <= 1) {
return 1;
}
double x
= (n * log10 (n / M_E) + log10 (2 * M_PI * n) / 2.0);
return floor (x) + 1;
}
int main()
{
cout << countDigitsInFactorial(1) << endl;
cout << countDigitsInFactorial(5) << endl;
cout << countDigitsInFactorial(10) << endl;
cout << countDigitsInFactorial(120) << endl;
return 0;
}
|
Java
import java.lang.Math;
import java.util.Scanner;
public class Solution {
public static int countDigitsInFactorial( int n) {
if (n < 0 ) {
return 0 ;
}
if (n <= 1 ) {
return 1 ;
}
double x = (n * Math.log10(n / Math.E) + Math.log10( 2 * Math.PI * n) / 2.0 );
return ( int ) Math.floor(x) + 1 ;
}
public static void main(String[] args) {
System.out.println(countDigitsInFactorial( 1 ));
System.out.println(countDigitsInFactorial( 5 ));
System.out.println(countDigitsInFactorial( 10 ));
System.out.println(countDigitsInFactorial( 120 ));
}
}
|
Python3
import math
def countDigitsInFactorial(n):
if n < 0 :
return 0
if n < = 1 :
return 1
x = (n * math.log10(n / math.e) + math.log10( 2 * math.pi * n) / 2.0 )
return math.floor(x) + 1
print (countDigitsInFactorial( 1 ))
print (countDigitsInFactorial( 5 ))
print (countDigitsInFactorial( 10 ))
print (countDigitsInFactorial( 120 ))
|
C#
using System;
public class Program {
static int CountDigitsInFactorial( int n)
{
if (n < 0) {
return 0;
}
if (n <= 1) {
return 1;
}
double x = (n * Math.Log10(n / Math.E)
+ Math.Log10(2 * Math.PI * n) / 2.0);
return ( int )Math.Floor(x) + 1;
}
public static void Main()
{
Console.WriteLine(CountDigitsInFactorial(1));
Console.WriteLine(CountDigitsInFactorial(5));
Console.WriteLine(CountDigitsInFactorial(10));
Console.WriteLine(CountDigitsInFactorial(120));
}
}
|
Javascript
function countDigitsInFactorial(n) {
if (n < 0) {
return 0;
}
if (n <= 1) {
return 1;
}
let x = n * Math.log10(n / Math.E) + Math.log10(2 * Math.PI * n) / 2.0;
return Math.floor(x) + 1;
}
console.log(countDigitsInFactorial(1));
console.log(countDigitsInFactorial(5));
console.log(countDigitsInFactorial(10));
console.log(countDigitsInFactorial(120));
|
Time complexity: O(1)
The time complexity of the above approach to count the number of digits in n! using Stirling’s approximation and logarithms is O(1), meaning it is constant time complexity.
Auxiliary space: O(1)
In the next set, we’d see how to further optimize our approach and reduce the time complexity for the same program.
Last Updated :
14 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...