- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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 Articles
- 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 maximum score from performing multiplication operations in Python
- Program to find maximize score after n operations in Python
- Program to find minimum possible maximum value after k operations in python
- Count number of step required to reduce N to 1 by following certain rule in C++
- Program to find number of operations needed to decrease n to 0 in C++
- C++ program to count minimum number of operations needed to make number n to 1
- Golang Program to find the minimum and maximum number, using binary operations.
- Maximum XOR using K numbers from 1 to n in C++
- Count the number of operations required to reduce the given number in C++
- Program to find maximum sum by performing at most k negate operations in Python
- Program to construct Maximum Stack with given operations in C++