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 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 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findDigits ( int n , int b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double x = ( ( n * log10 ( n / M_E ) + log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( log10 ( b ) ) ; return floor ( x ) + 1 ; } int main ( ) { cout << findDigits ( 4 , 16 ) << endl ; cout << findDigits ( 5 , 8 ) << endl ; cout << findDigits ( 12 , 16 ) << endl ; cout << findDigits ( 19 , 13 ) << endl ; return 0 ; }