Find the length of factorial of a number in any given base | A optimised program to find the number of digits in a factorial in base b ; Returns the number of digits present in n ! in base b Since the result can be large long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code ; calling findDigits ( Number , Base )
class GFG { static long findDigits ( int n , int b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double M_PI = 3.141592 ; double M_E = 2.7182 ; double x = ( ( n * Math . log10 ( n / M_E ) + Math . log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( Math . log10 ( b ) ) ; return ( long ) ( Math . floor ( x ) + 1 ) ; } public static void main ( String [ ] args ) { System . out . print ( findDigits ( 4 , 16 ) + "NEW_LINE"); System . out . print ( findDigits ( 5 , 8 ) + "NEW_LINE"); System . out . print ( findDigits ( 12 , 16 ) + "NEW_LINE"); System . out . print ( findDigits ( 19 , 13 ) + "NEW_LINE"); } }