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.
Input: n = 5
5 is the smallest prime divisor, thus it gets subtracted and n gets reduced to 0.
Input: n = 25
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
- 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:
- Count the number of operations required to reduce the given number
- Minimum number of operations required to reduce N to 1
- Minimum number of given operations required to reduce the array to 0 element
- Minimum operations of the given type required to make a complete graph
- Count the number of carry operations required to add two numbers
- Reduce N to 1 with minimum number of given operations
- Find maximum operations to reduce N to 1
- Reduce a number to 1 by performing given operations | Set 2
- Minimum steps required to reduce all the elements of the array to zero
- Minimum number operations required to convert n to m | Set-2
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Find the modified array after performing k operations of given type
- Make all the array elements odd with minimum operations of given type
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimum number of operations required to delete all elements of the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.