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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

## 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

Live Demo

// 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;
}
4