DEV Community

Cover image for Kamenetsky's Algorithm
Remon Hasan
Remon Hasan

Posted on

Kamenetsky's Algorithm

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

Sample Problem Link:

In this problem set you can use Kamnestsky's Algorithm

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!

Solve Link:

Top comments (0)