Find the length of factorial of a number in any given base | Returns the number of digits present in n ! in base b Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; calling findDigits ( Number , Base )
function findDigits ( n , b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; var M_PI = 3.141592 ; var M_E = 2.7182 ; var x = ( ( n * Math . log10 ( n / M_E ) + Math . log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( Math . log10 ( b ) ) ; return Math . floor ( x ) + 1 ; } document . write ( findDigits ( 4 , 16 ) + " " ) ; document . write ( findDigits ( 5 , 8 ) + " " ) ; document . write ( findDigits ( 12 , 16 ) + " " ) ; document . write ( findDigits ( 19 , 13 ) + " " ) ;