Posted on by Kalkicode
Code Mathematics

# Count digits in a factorial

Counting the number of digits in a factorial of a given number is an interesting problem that involves calculating the factorial and determining its digit count. A factorial of a non-negative integer 'n', denoted as 'n!', is the product of all positive integers from 1 to 'n'. In this article, we will explore how to count the digits in a factorial using a C program.

## Problem Statement

Given a positive integer 'n', the task is to find and count the number of digits in 'n!'.

## Example

For example, if 'n' is 5, then the factorial of 5 is 5! = 120, which has 3 digits. Similarly, for 'n' as 12, the factorial is 479,001,600, which has 9 digits.

## Idea to Solve

To count the digits in 'n!', we can calculate 'n!' and then determine the number of digits in the result. We can use the logarithmic property of the factorial to estimate the number of digits.

## Pseudocode

``````function factorial_digits(number):
If number < 0:
digit = 0
Else if number > 1:
Calculate result = (number * log10(number / M_E) + log10(2 * M_PI * number) / 2.0)
digit = floor(result) + 1
Print "Factorial of [", number, "]"
Print "Digit :", digit

function main():
factorial_digits(5)
factorial_digits(12)
factorial_digits(15)
factorial_digits(220)``````

## Algorithm Explanation

1. The `factorial_digits` function takes one parameter: 'number'.
2. If 'number' is less than 0, it sets digit to 0 (since factorials of negative numbers are undefined).
3. If 'number' is greater than 1, it calculates the result using the logarithmic property of the factorial formula.
4. It calculates the number of digits in 'result' using the floor of the logarithm and adds 1.
5. It prints the calculated factorial digit count for the given 'number'.

## Code Solution

``````// C program
// Count digits in a factorial
#include <stdio.h>
#include <math.h>

//Find digits of a factorial number
void factorial_digits(int number)
{
int digit = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
double result = ((number * log10(number / M_E) + log10(2 * M_PI * number) / 2.0));
digit = floor(result) + 1;
}
printf("\n [%d] Factorial digit : [%d]", number, digit);
}
int main()
{
//Test case
/*
Factorial of [5]
// 120
// Digit : 3
*/
factorial_digits(5);
/*
Factorial of [12]
// 479,001,600
// Digit : 9
*/
factorial_digits(12);
/*
Factorial of [15]
// 1307674368000
// Digit : 13
*/
factorial_digits(15);
/*
Factorial of [220]

//  2.283860335 E+421

2283860335914641457397265865
115333727042973071546228701773
634716126027692603024845877776
549791921102945706558196074779
575009550523224197049956176972
302056587667226166060976323404
977554732543013557133146825747
553799450849523377065894531021
055272516334278466875614904921
365807833845853428557155180084
957884822642989867003294551385
992993862178352349027264696691
854493614080000000000000000000
000000000000000000000000000000
0000
// Digit : 422

*/
factorial_digits(220);
return 0;
}``````

#### Output

`````` [5] Factorial digit : [3]
[12] Factorial digit : [9]
[15] Factorial digit : [13]
[220] Factorial digit : [422]``````
``````// Java program
// Count digits in a factorial
class FactorialNo
{
//Find digits of a factorial number
public void factorial_digits(int number)
{
int digit = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
double result = ((number * Math.log10(number / Math.E) + Math.log10(2 * Math.PI * number) / 2.0));
digit = (int)Math.floor(result) + 1;
}
System.out.print("\n [" + number + "] Factorial digit : [" + digit + "]");
}
public static void main(String[] args)
{
FactorialNo obj = new FactorialNo();
//Test case
obj.factorial_digits(5);
obj.factorial_digits(12);
obj.factorial_digits(15);
obj.factorial_digits(220);
}
}``````

#### Output

`````` [5] Factorial digit : [3]
[12] Factorial digit : [9]
[15] Factorial digit : [13]
[220] Factorial digit : [422]``````
``````//Include header file
#include <iostream>
#include<math.h>
using namespace std;

// C++ program
// Count digits in a factorial

class FactorialNo
{
public:
//Find digits of a factorial number
void factorial_digits(int number)
{
int digit = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
double result = ((number *log10(number / M_E) + log10(2  * M_PI * number) / 2.0));
digit = (int) floor(result) + 1;
}
cout << "\n [" << number << "] Factorial digit : [" << digit << "]";
}
};
int main()
{
FactorialNo obj = FactorialNo();
//Test case
obj.factorial_digits(5);
obj.factorial_digits(12);
obj.factorial_digits(15);
obj.factorial_digits(220);
return 0;
}``````

#### Output

`````` [5] Factorial digit : [3]
[12] Factorial digit : [9]
[15] Factorial digit : [13]
[220] Factorial digit : [422]``````
``````//Include namespace system
using System;
// C# program
// Count digits in a factorial
class FactorialNo
{
//Find digits of a factorial number
public void factorial_digits(int number)
{
int digit = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
double result = ((number * Math.Log10(number / Math.E) + Math.Log10(2 * Math.PI * number) / 2.0));
digit = (int) Math.Floor(result) + 1;
}
Console.Write("\n [" + number + "] Factorial digit : [" + digit + "]");
}
public static void Main(String[] args)
{
FactorialNo obj = new FactorialNo();
//Test case
obj.factorial_digits(5);
obj.factorial_digits(12);
obj.factorial_digits(15);
obj.factorial_digits(220);
}
}``````

#### Output

`````` [5] Factorial digit : [3]
[12] Factorial digit : [9]
[15] Factorial digit : [13]
[220] Factorial digit : [422]``````
``````<?php
// Php program
// Count digits in a factorial
class FactorialNo
{
//Find digits of a factorial number
public	function factorial_digits(\$number)
{
\$digit = 1;
if (\$number < 0)
{
//When get negative number
\$digit = 0;
}
else if (\$number > 1)
{
//Find digit
\$result = ((\$number * log10(intval(\$number / M_E)) + intval(log10(2 * M_PI * \$number) / 2.0)));
\$digit = (floor(\$result)) + 1;
}
echo "\n [". \$number ."] Factorial digit : [". \$digit ."]";
}
}

function main()
{
\$obj = new FactorialNo();
//Test case
\$obj->factorial_digits(5);
\$obj->factorial_digits(12);
\$obj->factorial_digits(15);
\$obj->factorial_digits(220);
}
main();``````

#### Output

`````` [5] Factorial digit : [1]
[12] Factorial digit : [8]
[15] Factorial digit : [11]
[220] Factorial digit : [420]``````
``````// Node Js program
// Count digits in a factorial
class FactorialNo
{
//Find digits of a factorial number
factorial_digits(number)
{
var digit = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
var result = ((number * Math.log10(parseInt(number / Math.E)) + parseInt(Math.log10(2 * Math.PI * number) / 2.0)));
digit = (Math.floor(result)) + 1;
}
process.stdout.write("\n [" + number + "] Factorial digit : [" + digit + "]");
}
}

function main()
{
var obj = new FactorialNo();
//Test case
obj.factorial_digits(5);
obj.factorial_digits(12);
obj.factorial_digits(15);
obj.factorial_digits(220);
}
main();``````

#### Output

`````` [5] Factorial digit : [1]
[12] Factorial digit : [8]
[15] Factorial digit : [11]
[220] Factorial digit : [420]``````
``````import math
#  Python 3 program
#  Count digits in a factorial
class FactorialNo :
# Find digits of a factorial number
def factorial_digits(self, number) :
digit = 1
if (number < 0) :
# When get negative number
digit = 0

elif(number > 1) :
# Find digit
result = ((number * math.log10(int(number / math.e)) + int(math.log10(2 * math.pi * number) / 2.0)))
digit = (math.floor(result)) + 1

print("\n [{0}] Factorial digit : [{1}]".format(number,digit), end = "")

def main() :
obj = FactorialNo()
# Test case
obj.factorial_digits(5)
obj.factorial_digits(12)
obj.factorial_digits(15)
obj.factorial_digits(220)

if __name__ == "__main__": main()``````

#### Output

`````` [5] Factorial digit : [1]
[12] Factorial digit : [8]
[15] Factorial digit : [11]
[220] Factorial digit : [420]``````
``````#  Ruby program
#  Count digits in a factorial
class FactorialNo
# Find digits of a factorial number
def factorial_digits(number)
digit = 1
if (number < 0)
# When get negative number
digit = 0
elsif(number > 1)
# Find digit
result = ((number * Math.log10(number / Math::E) + Math.log10(2 * Math::PI * number) / 2.0))
digit = (result.floor()).to_i + 1
end

print("\n [", number ,"] Factorial digit : [", digit ,"]")
end

end

def main()
obj = FactorialNo.new()
# Test case
obj.factorial_digits(5)
obj.factorial_digits(12)
obj.factorial_digits(15)
obj.factorial_digits(220)
end

main()``````

#### Output

`````` [5] Factorial digit : [3]
[12] Factorial digit : [9]
[15] Factorial digit : [13]
[220] Factorial digit : [422]``````
``````// Scala program
// Count digits in a factorial
class FactorialNo
{
//Find digits of a factorial number
def factorial_digits(number: Int): Unit = {
var digit: Int = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
var result: Double = ((number * Math.log10((number / Math.E).toInt) + (Math.log10(2 * Math.PI * number) / 2.0).toInt));
digit = (Math.floor(result)).toInt + 1;
}
print("\n [" + number + "] Factorial digit : [" + digit + "]");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: FactorialNo = new FactorialNo();
//Test case
obj.factorial_digits(5);
obj.factorial_digits(12);
obj.factorial_digits(15);
obj.factorial_digits(220);
}
}``````

#### Output

`````` [5] Factorial digit : [1]
[12] Factorial digit : [8]
[15] Factorial digit : [11]
[220] Factorial digit : [420]``````
``````import Foundation
// Swift 4 program
// Count digits in a factorial
class FactorialNo
{
//Find digits of a factorial number
func factorial_digits(_ number: Int)
{
var digit: Int = 1;
if (number < 0)
{
//When get negative number
digit = 0;
}
else if (number > 1)
{
//Find digit
let result: Double = ((Double(number) * log10(Double(number) / M_E) + log10(2 * Double.pi * Double(number)) / 2.0));
digit = Int(floor(result)) + 1;
}
print("\n [\(number)] Factorial digit : [\(digit)] ", terminator: "");
}
}
func main()
{
let obj: FactorialNo = FactorialNo();
//Test case
obj.factorial_digits(5);
obj.factorial_digits(12);
obj.factorial_digits(15);
obj.factorial_digits(220);
}
main();``````

#### Output

`````` [5] Factorial digit : [3]
[12] Factorial digit : [9]
[15] Factorial digit : [13]
[220] Factorial digit : [422]``````

## Time Complexity

The time complexity of counting the digits in a factorial using the logarithmic property is indeed O(1).

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post