Algorithm
Problem link- https://www.spoj.com/problems/LENGFACT/
LENGFACT - Factorial length
Given integer n, print length of n! (which is factorial of n).
Input
The first line of the standard input contains one integer t (t < 10001) which is the number of test cases.
In each of the next t lines there is number n (0 <= n <= 5*10^9).
Output
For each test, print the length of n! (which is factorial of n).
Example
Input:
3
1
10
100
Output:
1
7
158
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
import math
T = int(input())
for _ in range(T):
N = int(input())
if N < 2:
print(1)
else:
print(math.floor((math.log(2 * math.pi * N) / 2 + \
N * (math.log(N) - 1)) / math.log(10) \
) + 1)
Copy The Code &
Try With Live Editor
Input
1
10
100
Output
7
158
#2 Code Example with C++ Programming
Code -
C++ Programming
#include<iostream7gt;
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
double n;
double pi = 2*acos(0);
long long int len,l;
while(t-->0)
{
cin>>n;
if(n==0.00||n==1.00)l=1;
else{
l=(((log(2.00*pi*n)/2.00)+(n*(log(n)-1.00)))/log(10.0))+1;
}
cout<<l<<endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1
10
100
Output
7
158
Demonstration
SPOJ Solution-Factorial length-Solution in C, C++, Java, Python