- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 Articles
- 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 maximum score from performing multiplication operations in Python
- Count operations of the given type required to reduce N to 0 in C++
- Program to find maximize score after n operations in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find maximum sum by performing at most k negate operations in Python
- String Operations in Python\n
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Program to find all missing numbers from 1 to N in Python
- Find the minimum and maximum amount to buy all N candies in Python
- Find the maximum repeating number in O(n) time and O(1) extra space 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++