Problem 2: Digits in Factorial
Points:
Small Case: 10 Answer: 3223
Large Case: 20 Answer: 11565705518104
Factorial of a number 'n' is defined by
n!= 1 x 2 x 3 x 4 x ....... x n
The problem is to find the number of digits in n!.
Small Case: n=1215
Large Case: n=10^12
Solution:
n! can be expressed by stirling formula as
Total number of digit in a number x can be given by
floor(log(x))+1 where log is calculated in base 10.
Hence,the number if digits in n! is
floor(log(n!))+1
#include<iostream>
#include<cmath>
using namespace std;
#define PI 3.141592653589793
int main()
{
long long n;
long long digit;
cin>>n;
digit = floor(log10(pow(2*PI*n,.5))+(double)n*(log10((double)n)-((double)1/log(10))))+1;
cout<<digit<<"\n";
}
Submit your solution
You need to be logged in to submit a solution.
discussions
There is no discussion yet. Feel free to ask an question you might have regarding this problem
post a question
Loading...



