Suppose we have two numbers P and Q and they form a number N = (P!/Q!). We have to reduce N to 1 by performing maximum number of operations possible. In each operation, one can replace N with N/X when N is divisible by X. We will return the maximum number of operations that can be possible.
So, if the input is like A = 7, B = 4, then the output will be 4 as N is 210 and the divisors are 2, 3, 5, 7.
To solve this, we will follow these steps −
N := 1000005
factors := an array of size N and fill with 0
From the main method, do the following −
for i in range 2 to N, do
if factors[i] is same as 0, then
for j in range i to N, update in each step by i, do
factors[j] := factors[j / i] + 1
for i in range 1 to N, do
factors[i] := factors[i] + factors[i - 1];
return factors[a] - factors[b]
Example
Let us see the following implementation to get better understanding −
N = 1000005 factors = [0] * N; def get_prime_facts() : for i in range(2, N) : if (factors[i] == 0) : for j in range(i, N, i) : factors[j] = factors[j // i] + 1 for i in range(1, N) : factors[i] += factors[i - 1]; get_prime_facts(); a = 7; b = 4; print(factors[a] - factors[b])
Input
7,4
Output
4