# Find maximum operations to reduce N to 1

Given two numbers A and B ( A and B can be up to 10^{6} ) which forms a number **N = (A!/B!)**. The task is to **reduce N to 1** by performing **maximum **number of operations possible.

In each operation, one can replace N with **N/X** if N is divisible by X.

Find the maximum number of operations that can be possible.

**Examples:**

Input :A = 6, B = 3Output :5Explanation :N is 120 and the divisors are 2, 2, 2, 3, 5Input :A = 2, B = 1Output :1Explanation :N is 2 and the divisor is 2.

Observe that factorization of number A!/B! is this same as factorization of numbers (B + 1)*(B + 2)*…*(A – 1)*A.

Also, the number of operations will be maximum if we divide N with only with it’s prime factors. So, in other words we need to find the count of prime factors of N including duplicates.

Let’s count the number of prime factors in the factorization of every number from 2 to 1000000.

First, use Sieve of Eratosthenes to find a prime divisor of each of these numbers. Then we can calculate the number of prime factors in the factorization of a using the formula:

primefactors[num] = primefactors[num / primediviser[num]] + 1

Now, one can use prefix sum array for prime factors and then answer for the sum on an interval [A, B].

Below is the implementation of the above approach:

## C++

`// CPP program to find maximum ` `// number moves possible ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 1000005 ` ` ` `// To store number of prime ` `// factors of each number ` `int` `primeFactors[N]; ` ` ` `// Function to find number of prime ` `// factors of each number ` `void` `findPrimeFactors() ` `{ ` ` ` `for` `(` `int` `i = 2; i < N; i++) ` ` ` `// if i is a prime number ` ` ` `if` `(primeFactors[i] == 0) ` ` ` `for` `(` `int` `j = i; j < N; j += i) ` ` ` `// increase value by one from ` ` ` `// it's preveious multiple ` ` ` `primeFactors[j] = primeFactors[j / i] + 1; ` ` ` ` ` `// make prefix sum ` ` ` `// this will be helpful for ` ` ` `// multiple test cases ` ` ` `for` `(` `int` `i = 1; i < N; i++) ` ` ` `primeFactors[i] += primeFactors[i - 1]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Generate primeFactors array ` ` ` `findPrimeFactors(); ` ` ` ` ` `int` `a = 6, b = 3; ` ` ` ` ` `// required answer ` ` ` `cout << primeFactors[a] - primeFactors[b]; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum ` `// number moves possible ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `N= ` `1000005` `; ` ` ` `// To store number of prime ` `// factors of each number ` `static` `int` `primeFactors[] = ` `new` `int` `[N]; ` ` ` `// Function to find number of prime ` `// factors of each number ` `static` `void` `findPrimeFactors() ` `{ ` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++) ` ` ` ` ` `// if i is a prime number ` ` ` `if` `(primeFactors[i] == ` `0` `) ` ` ` `for` `(` `int` `j = i; j < N; j += i) ` ` ` ` ` `// increase value by one from ` ` ` `// it's preveious multiple ` ` ` `primeFactors[j] = primeFactors[j / i] + ` `1` `; ` ` ` ` ` `// make prefix sum ` ` ` `// this will be helpful for ` ` ` `// multiple test cases ` ` ` `for` `(` `int` `i = ` `1` `; i < N; i++) ` ` ` `primeFactors[i] += primeFactors[i - ` `1` `]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `// Generate primeFactors array ` ` ` `findPrimeFactors(); ` ` ` `int` `a = ` `6` `, b = ` `3` `; ` ` ` ` ` `// required answer ` ` ` `System.out.println (primeFactors[a] - ` ` ` `primeFactors[b]); ` `} ` `} ` ` ` `// This code is contributed by jit_t. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum ` `# number moves possible ` `N ` `=` `1000005` ` ` `# To store number of prime ` `# factors of each number ` `primeFactors ` `=` `[` `0` `] ` `*` `N; ` ` ` `# Function to find number of prime ` `# factors of each number ` `def` `findPrimeFactors() : ` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, N) : ` ` ` ` ` `# if i is a prime number ` ` ` `if` `(primeFactors[i] ` `=` `=` `0` `) : ` ` ` `for` `j ` `in` `range` `(i, N, i) : ` ` ` ` ` `# increase value by one from ` ` ` `# it's preveious multiple ` ` ` `primeFactors[j] ` `=` `primeFactors[j ` `/` `/` `i] ` `+` `1` `; ` ` ` ` ` `# make prefix sum this will be ` ` ` `# helpful for multiple test cases ` ` ` `for` `i ` `in` `range` `(` `1` `, N) : ` ` ` `primeFactors[i] ` `+` `=` `primeFactors[i ` `-` `1` `]; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Generate primeFactors array ` ` ` `findPrimeFactors(); ` ` ` ` ` `a ` `=` `6` `; b ` `=` `3` `; ` ` ` ` ` `# required answer ` ` ` `print` `(primeFactors[a] ` `-` `primeFactors[b]); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum ` `// number moves possible ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `N = 1000005; ` ` ` ` ` `// To store number of prime ` ` ` `// factors of each number ` ` ` `static` `int` `[]primeFactors = ` `new` `int` `[N]; ` ` ` ` ` `// Function to find number of prime ` ` ` `// factors of each number ` ` ` `static` `void` `findPrimeFactors() ` ` ` `{ ` ` ` `for` `(` `int` `i = 2; i < N; i++) ` ` ` ` ` `// if i is a prime number ` ` ` `if` `(primeFactors[i] == 0) ` ` ` `for` `(` `int` `j = i; j < N; j += i) ` ` ` ` ` `// increase value by one from ` ` ` `// it's preveious multiple ` ` ` `primeFactors[j] = primeFactors[j / i] + 1; ` ` ` ` ` `// make prefix sum ` ` ` `// this will be helpful for ` ` ` `// multiple test cases ` ` ` `for` `(` `int` `i = 1; i < N; i++) ` ` ` `primeFactors[i] += primeFactors[i - 1]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` ` ` `// Generate primeFactors array ` ` ` `findPrimeFactors(); ` ` ` `int` `a = 6, b = 3; ` ` ` ` ` `// required answer ` ` ` `Console.WriteLine(primeFactors[a] - ` ` ` `primeFactors[b]); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

**Output:**

5

## Recommended Posts:

- Minimum number of operations required to reduce N to 1
- Count operations of the given type required to reduce N to 0
- Maximum value in an array after m range increment operations
- Maximum sum of all elements of array after performing given operations
- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Find minimum operations needed to make an Array beautiful
- Find the number of operations required to make all array elements Equal
- Find minimum number of merge operations to make an array palindrome
- Reduce the array to a single element with the given operation
- Count number of step required to reduce N to 1 by following certain rule
- Reduce the string by removing K consecutive identical characters
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find maximum value of x such that n! % (k^x) = 0

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.