Find the length of factorial of a number in any given base

Given an integer n and base B, the task is to find the length of n! in base B.
Examples:

Input: n = 4, b = 10
Output: 2
Explanation: 4! = 24, hence number of digits is 2

Input: n = 4, b = 16
Output: 2
Explanation: 4! = 18 in base 16, hence number of digits is 2

Approach:
In order to solve the problem we use Kamenetsky’s formula which approximates the number of digits in a factorial



f(x) =    log10( ((n/e)^n) * sqrt(2*pi*n))

The number of digits in n to the base b is given by logb(n) = log10(n) / log10(b). Hence, by using properties of logarithms, the number of digits of factorial in base b can be obtained by

f(x) = ( n* log10(( n/ e)) + log10(2*pi*n)/2  ) / log10(b)

This approach can deal with large inputs that can be accommodated in a 32-bit integer and even beyond that!

Below code is the implementation of above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A optimised program to find the 
// number of digits in a factorial in base b
#include <bits/stdc++.h>
using namespace std;
  
// Returns the number of digits present 
// in n! in base b Since the result can be large
// long long is used as return type
long long findDigits(int n, int b)
{
    // factorial of -ve number 
    // doesn't exists
    if (n < 0)
        return 0;
  
    // base case
    if (n <= 1)
        return 1;
  
    // Use Kamenetsky formula to calculate
    // the number of digits
    double x = ((n * log10(n / M_E) + 
                log10(2 * M_PI * n) /
                2.0)) / (log10(b));
  
    return floor(x) + 1;
}
  
// Driver Code
int main()
{
    //calling findDigits(Number, Base)
    cout << findDigits(4, 16) << endl;
    cout << findDigits(5, 8) << endl;
    cout << findDigits(12, 16) << endl;
    cout << findDigits(19, 13) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A optimised program to find the 
// number of digits in a factorial in base b
class GFG{
   
// Returns the number of digits present 
// in n! in base b Since the result can be large
// long is used as return type
static long findDigits(int n, int b)
{
    // factorial of -ve number 
    // doesn't exists
    if (n < 0)
        return 0;
   
    // base case
    if (n <= 1)
        return 1;
    double M_PI = 3.141592;
    double M_E = 2.7182;
      
    // Use Kamenetsky formula to calculate
    // the number of digits
    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);
}
   
// Driver Code
public static void main(String[] args)
{
    //calling findDigits(Number, Base)
    System.out.print(findDigits(4, 16) +"\n");
    System.out.print(findDigits(5, 8) +"\n");
    System.out.print(findDigits(12, 16) +"\n");
    System.out.print(findDigits(19, 13) +"\n");
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

from math import log10,floor
  
# 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
def findDigits(n, b):
      
    # factorial of -ve number 
    # doesn't exists
    if (n < 0):
        return 0
      
    M_PI = 3.141592
    M_E = 2.7182
  
    # base case
    if (n <= 1):
        return 1
  
    # Use Kamenetsky formula to calculate
    # the number of digits
    x = ((n * log10(n / M_E) + log10(2 * M_PI * n) / 2.0)) / (log10(b))
  
    return floor(x) + 1
  
# Driver Code
if __name__ == '__main__':
      
    #calling findDigits(Number, Base)
    print(findDigits(4, 16))
    print(findDigits(5, 8))
    print(findDigits(12, 16))
    print(findDigits(19, 13))
  
# This code is contributed by Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A optimised C# program to find the 
// number of digits in a factorial in base b 
using System;
  
class GFG{ 
      
    // Returns the number of digits present 
    // in n! in base b Since the result can be large 
    // long is used as return type 
    static long findDigits(int n, int b) 
    
        // factorial of -ve number 
        // doesn't exists 
        if (n < 0) 
            return 0; 
      
        // base case 
        if (n <= 1) 
            return 1; 
        double M_PI = 3.141592; 
        double M_E = 2.7182; 
          
        // Use Kamenetsky formula to calculate 
        // the number of digits 
        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); 
    
      
    // Driver Code 
    public static void Main(string[] args) 
    
        // calling findDigits(Number, Base) 
        Console.WriteLine(findDigits(4, 16)); 
        Console.WriteLine(findDigits(5, 8)); 
        Console.WriteLine(findDigits(12, 16)); 
        Console.WriteLine(findDigits(19, 13)); 
    
  
// This code is contributed by Yash_R

chevron_right


Output:

2
3
8
16

Reference:oeis.org

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts: