Given an integer **n**. The task is to count the number of operations required to reduce **n** to **0**. In every operation, **n** can be updated as **n = n – d** where **d** is the smallest prime divisor of **n**.

**Examples:**

Input:n = 5

Output:1

5 is the smallest prime divisor, thus it gets subtracted and n gets reduced to 0.

Input:n = 25

Output:11

5 is the smallest prime divisor, thus it gets subtracted and n gets reduced to 20. Then 2 is the smallest divisor and so on.

Input:n = 4

Output:2

**Approach:**

- When
**n**is**even**then the smallest prime divisor of**n**will be**2**and subtracting**2**from**n**will again give an even integer i.e. gain**2**will be chosen as the smallest prime divisor and these steps will repeat until**n**gets reduced to**0**. - When
**n**is**odd**then the smallest prime divisor of**n**will also be odd and subtracting an odd integer from another odd integer will give an even integer as the result and then the result can be found out by repeating step 1 for the current even integer. - Thus, the task is to find the smallest divisor
**d**, subtract it,**n = n – d**and print**1 + ((n – d) / 2)**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required ` `// number of operations ` `int` `countOperations (` `int` `n) ` `{ ` ` ` `int` `i = 2; ` ` ` ` ` `// Finding the smallest divisor ` ` ` `while` `((i * i) < n && (n % i)) ` ` ` `i += 1; ` ` ` ` ` `if` `((i * i) > n) ` ` ` `i = n; ` ` ` ` ` `// Return the count of operations ` ` ` `return` `(1 + (n - i)/2); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << countOperations(n); ` `} ` ` ` `//This code is contributed by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the required ` `// number of operations ` `static` `int` `countOperations (` `int` `n) ` `{ ` ` ` `int` `i = ` `2` `; ` ` ` ` ` `// Finding the smallest divisor ` ` ` `while` `((i * i) < n && (n % i) > ` `0` `) ` ` ` `i += ` `1` `; ` ` ` ` ` `if` `((i * i) > n) ` ` ` `i = n; ` ` ` ` ` `// Return the count of operations ` ` ` `return` `(` `1` `+ (n - i) / ` `2` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(countOperations(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required ` `# number of operations ` `def` `countOperations(n): ` ` ` `i ` `=` `2` ` ` ` ` `# Finding the smallest divisor ` ` ` `while` `((i ` `*` `i) < n ` `and` `(n ` `%` `i)): ` ` ` `i ` `+` `=` `1` ` ` ` ` `if` `((i ` `*` `i) > n): ` ` ` `i ` `=` `n ` ` ` ` ` `# Return the count of operations ` ` ` `return` `(` `1` `+` `(n ` `-` `i)` `/` `/` `2` `) ` ` ` `# Driver code ` `n ` `=` `5` `print` `(countOperations(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to return the required ` `// number of operations ` `static` `int` `countOperations (` `int` `n) ` `{ ` ` ` `int` `i = 2; ` ` ` ` ` `// Finding the smallest divisor ` ` ` `while` `((i * i) < n && (n % i) > 0) ` ` ` `i += 1; ` ` ` ` ` `if` `((i * i) > n) ` ` ` `i = n; ` ` ` ` ` `// Return the count of operations ` ` ` `return` `(1 + (n - i) / 2); ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(countOperations(n)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the required ` `// number of operations ` `function` `countOperations(` `$n` `) ` `{ ` ` ` `$i` `= 2; ` ` ` ` ` `# Finding the smallest divisor ` ` ` `while` `((` `$i` `* ` `$i` `) < ` `$n` `and` `(` `$n` `% ` `$i` `)) ` ` ` `$i` `+= 1; ` ` ` ` ` `if` `((` `$i` `* ` `$i` `) > ` `$n` `) ` ` ` `$i` `= ` `$n` `; ` ` ` ` ` `# Return the ` `count` `of operations ` ` ` `return` `1 + ` `floor` `((` `$n` `- ` `$i` `) / 2); ` `} ` ` ` `// Driver code ` `$n` `= 5 ; ` `echo` `countOperations(` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the number of operations required to reduce the given number
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to reduce N to 0
- Minimum operations of the given type required to make a complete graph
- Reduce a given number to form a key by the given operations
- Minimize count of given operations required to make two given strings permutations of each other
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Reduce N to 1 with minimum number of given operations
- Reduce a number to 1 by performing given operations | Set 2
- Minimum steps to reduce N to 0 by given operations
- Reduce N to 0 or less by given X and Y operations
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimum steps required to reduce all the elements of the array to zero
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Find maximum operations to reduce N to 1
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Min operations to reduce N by multiplying by any number or taking square root

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.