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:Â n = 5
Output: 3
Explanation: 5! = 120, i.e., 3 digitsInput: n = 10
Output: 7
Explanation: 10! = 3628800, i.e., 7 digits
Table of Content
[Naive approach] Calculate factorial of Number - O(n) Time and O(1) Space
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 .
#include <iostream>
using namespace std;
int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
int digitsInFactorial(int n) {
// Factorial exists only for n >= 0
if (n < 0)
return 0;
// Calculate factorial
int fact = factorial(n);
// Count digits
int digits = 0;
do {
digits++;
fact /= 10;
} while (fact != 0);
return digits;
}
int main() {
cout << digitsInFactorial(12) << endl;
return 0;
}
public class GFG {
public static int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
public static int digitsInFactorial(int n) {
// Factorial exists only for n >= 0
if (n < 0)
return 0;
// Calculate factorial
int fact = factorial(n);
// Count digits
int digits = 0;
do {
digits++;
fact /= 10;
} while (fact!= 0);
return digits;
}
public static void main(String[] args) {
System.out.println(digitsInFactorial(12));
}
}
def factorial(n):
fact = 1
for i in range(2, n + 1):
fact *= i
return fact
def digitsInFactorial(n):
# Factorial exists only for n >= 0
if n < 0:
return 0
# Calculate factorial
fact = factorial(n)
# Count digits
digits = 0
while fact!= 0:
digits += 1
fact //= 10
return digits
if __name__ == "__main__":
print(digitsInFactorial(12))
using System;
public class GFG {
public static int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
public static int digitsInFactorial(int n) {
// Factorial exists only for n >= 0
if (n < 0)
return 0;
// Calculate factorial
int fact = factorial(n);
// Count digits
int digits = 0;
do {
digits++;
fact /= 10;
} while (fact!= 0);
return digits;
}
public static void Main() {
Console.WriteLine(digitsInFactorial(12));
}
}
function factorial(n) {
let fact = 1;
for (let i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
function digitsInFactorial(n) {
// Factorial exists only for n >= 0
if (n < 0)
return 0;
// Calculate factorial
let fact = factorial(n);
// Count digits
let digits = 0;
do {
digits++;
fact = Math.floor(fact / 10);
} while (fact!= 0);
return digits;
}
// Driver code
console.log(digitsInFactorial(12));
Output
9
[Better approach] Using logarithmic property - O(n) Time and O(1) space
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.
#include <iostream>
#include <cmath>
using namespace std;
int digitsInFactorial(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;
}
int main()
{
cout << digitsInFactorial(12) << endl;
return 0;
}
class GFG {
static int digitsInFactorial(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;
}
public static void main(String[] args)
{
System.out.println(digitsInFactorial(12));
}
}
import math
def digitsInFactorial(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
if __name__ == "__main__":
print(digitsInFactorial(12))
using System;
class GFG {
static int digitsInFactorial(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;
}
public static void Main()
{
Console.Write(digitsInFactorial(12) + "\n");
}
}
function digitsInFactorial(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
console.log(digitsInFactorial(12));
Output
9
[Expected Approach] Using Stirling's approximation formula - O(1) Time and O(1) Space
Stirling's approximation estimates the value of a factorial using the formula:
n! \approx \sqrt{2\pi n}\left(\frac{n}{e}\right)^n Instead of computing the factorial directly, the logarithm of this approximation is used. Since the number of digits of a number x is given by â log x base 10 â +1, taking the base-10 logarithm of Stirling's formula provides the digit count efficiently. This method avoids factorial computation, prevents overflow, and works efficiently even for very large values of n.
#include <cmath>
#include <iostream>
using namespace std;
int digitsInFactorial(int n)
{
// factorial exists only for n>=0
if (n < 0)
{
return 0;
}
if (n <= 1)
{
return 1;
}
// Calculating the digit's values
double x = (n * log10(n / M_E) + log10(2 * M_PI * n) / 2.0);
// returning the floor value + 1
return floor(x) + 1;
}
int main()
{
cout << digitsInFactorial(12) << endl;
return 0;
}
public class GFG {
public static int digitsInFactorial(int n)
{
// factorial exists only for n>=0
if (n < 0) {
return 0;
}
if (n <= 1) {
return 1;
}
// Calculating the digit's values
double x = (n * Math.log10(n / Math.E)
+ Math.log10(2 * Math.PI * n) / 2.0);
// returning the floor value + 1
return (int)Math.floor(x) + 1;
}
public static void main(String[] args)
{
System.out.println(digitsInFactorial(12));
}
}
import math
def digitsInFactorial(n):
# factorial exists only for n>=0
if n < 0:
return 0
if n <= 1:
return 1
# // Calculating the digit's values
x = (n * math.log10(n / math.e) + math.log10(2 * math.pi * n) / 2.0)
# returning the floor value + 1
return math.floor(x) + 1
# Testing the function with sample inputs
if __name__ == "__main__":
print(digitsInFactorial(12))
using System;
public class GFG {
static int digitsInFactorial(int n)
{
// factorial exists only for n>=0
if (n < 0) {
return 0;
}
if (n <= 1) {
return 1;
}
// Calculating the digit's values
double x = (n * Math.Log10(n / Math.E)
+ Math.Log10(2 * Math.PI * n) / 2.0);
// returning the floor value + 1
return (int)Math.Floor(x) + 1;
}
public static void Main()
{
Console.WriteLine(digitsInFactorial(12));
}
}
function digitsInFactorial(n) {
// factorial exists only for n>=0
if (n < 0) {
return 0;
}
if (n <= 1) {
return 1;
}
// Calculating the digit's values
let x = n * Math.log10(n / Math.E) + Math.log10(2 * Math.PI * n) / 2.0;
// returning the floor value + 1
return Math.floor(x) + 1;
}
// Driver Code
console.log(digitsInFactorial(12));
Output
9