This problem can be solved using Legendre’s Formula and Prime Factorization.
Legendre's Formula: Let be a prime number, and we call it then the exponent of in the prime factorization of is:
until is less than or equal to
When is not a prime, then we need to express the composite number as a product of its prime factors (Prime factorization). Let's say, = where are distinct prime factors of and are the corresponding exponents.
For each prime factor (where ) we can use Legendre's formula to find the exponent of in the prime factorization of Then the largest exponent of that divide would be the minimum of divided by
Now, what is the largest exponent of that divides
For each prime factor (where ) we can apply Legendre's formula to find the exponent of in the prime factorization of and separately. Then subtract the exponents we get for and from the exponent for to find the exponent of in the prime factorization of
So, the largest exponent of that divides would be:
Time and Space complexity: To avoid worst-case time complexity, use Sieve of Eratosthenes algorithm to store all primes up to facilitating efficient prime factorization of
Let, then to store all prime up to using Sieve of Eratosthenes:
Time complexity,
Space complexity,
Let, be the number of primes which are less than or equal to Then in each test cases, prime factorization of using stored primes:
Time complexity,
In the worst case, can be at most (Number of primes less than or equal to ). However, it’s still enough to pass within the given time limit.
So, the overall complexity of our solution:
Time complexity,
Space complexity,
All other complexities are small enough to be neglected.