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

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

- Related Questions & Answers
- Find maximum operations to reduce N to 1 in C++
- Program to find minimum operations to reduce X to zero in Python
- Reduce a number to 1 by performing given operations in C++
- Program to find maximize score after n operations in Python
- Program to find duplicate element from n+1 numbers ranging from 1 to n 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 minimum possible maximum value after k operations in python
- Program to find all missing numbers from 1 to N 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++
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Program to find maximum sum by performing at most k negate operations in Python