- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find maximum operations to reduce N to 1 in C++

## Concept

With respect of given two numbers P and Q ( P and Q can be up to 10^6 ) which forms a number N = (P!/Q!). Our task is to reduce N to 1 by performing maximum number of operations possible. Remember, in each operation, one can replace N with N/X if N is divisible by X. Determine the maximum number of operations that can be possible.

## Input

A = 7, B = 4

## Output

4

## Explanation

N is 210 and the divisors are 2, 3, 5, 7

## Input

A = 3, B = 1

## Output

2

## Explanation

N is 6 and the divisor are 2,3.

## Method

It has been observed that factorization of number P!/Q! is this same as factorization of numbers (Q + 1)*(Q + 2)*…*(P – 1)*P.

It should be noted also, the number of operations will be maximum if we divide N with only with its prime factors. As a result of this, in other words we need to determine the count of prime factors of N which include duplicates also.

Assume count the number of prime factors in the factorization of every number from 2 to 1000000.

At first, implement **Sieve of Eratosthenes** to determine a prime divisor of each of these numbers.It is explained as follows −

Build a list of consecutive integers from 2 to N: (2, 3, 4, …, N).

At first, assume p equal 2, the first prime number.

Beginning from p^2, count up in increments of p and indicate each of these numbers larger than or equal to p^2 itself in the list. So, these numbers can be p(p+1), p(p+2), p(p+3), etc..

Determine the first number larger than p in the list that is not indicated. It has beenseen that if there was no such number, stop. Else, assume p now equal to this number (which indicates the next prime), and again repeat from step 3.

After implementing Sieve of Eratosthenes method we can compute the number of prime factors in the factorization of a implementing the following formula −

primefactors[num] = primefactors[num / primedivisor[num]] + 1 At present, one can implement prefix sum array for prime factors and then answer for the sum on an interval [P, Q].

## Example

// CPP program to find maximum // number moves possible #include <bits/stdc++.h> using namespace std; #define N 1000005 // Used to store number of prime // factors of each number int primeFactors1[N]; // Shows Function to find number of prime // factors of each number void findPrimeFactors(){ for (int a = 2; a < N; a++) // Now if a is a prime number if (primeFactors1[a] == 0) for (int b = a; b < N; b += a) // Now increase value by one from // it's preveious multiple primeFactors1[b] = primeFactors1[b / a] + 1; // Build prefix sum // this will be helpful for // multiple test cases for (int a = 1; a < N; a++) primeFactors1[a] += primeFactors1[a - 1]; } // Driver Code int main(){ // Create primeFactors1 array findPrimeFactors(); int P = 7, Q = 4; // required answer cout << primeFactors1[P] - primeFactors1[Q]; return 0; }

## Output

4

- Related Questions & Answers
- Find maximum operations to reduce N to 1 in Python
- Reduce a number to 1 by performing given operations in C++
- Program to find minimum operations to reduce X to zero in Python
- Count operations of the given type required to reduce N to 0 in C++
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Program to find maximize score after n operations in Python
- Count number of step required to reduce N to 1 by following certain rule in C++
- Maximum XOR using K numbers from 1 to n in C++
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- A data structure for n elements and O(1) operations?
- Program to find maximum score from performing multiplication operations in Python
- Find the only repetitive element between 1 to n-1 using C++
- Program to find number of operations needed to decrease n to 0 in C++