
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count operations of the given type required to reduce N to 0 in C++
We are given a positive integer N. The goal is to find the number of operations required to reduce N to 0. Operation applied is N=N-P where P is the smallest prime divisor of P.
Let us understand with examples
Input − N=17
Output − Count of operations of the given type required to reduce N to 0 are − 1
Explanation − The smallest prime divisor of 17 is 17 itself. So the operation is applied only once 17-17=0.
Input − N=20
Output− Count of operations of the given type required to reduce N to 0 are − 10
Explanation − The smallest prime divisor of 20 is 2. Subtracting 2 again and again and finding next prime divisor:
20%2==0, 20-2=18 18%2==0, 18-2=16 …………………..14, 12, 10, 8, 6, 4, 2, 0 Total 10 times the operation is applied.
The approach used in the below program is as follows
For all even N’s the smallest prime divisor will always be 2 and subtracting 2 from even N will again produce an even number. For all odd numbers the smallest prime divisor will be odd, after subtracting odd from odd and number will become even so again 2 will become the smallest prime divisor. To find smallest prime divisor start from i=2 to i such that i*i<N and N%i==0. Total operations then would be count=1 + (N-i)/2.
Take an integer N as input.
Function N_to_Zero(int N) takes N and returns the number of operations required to reduce N to 0.
Take the initial value of count as 0.
Starting from i=2. Start traversing while (i*i)<N and N is not divisible by i (N%i!=0). Increment i.
If (i*i) exceeds N, set i=N.
The number of operations will be 1+(N-i)/2.
Set count as 1+(N-i)/2.
Return count as result.
Example
#include<bits/stdc++.h> using namespace std; int N_to_Zero(int N){ int count = 0; int i = 2; while((i * i) < N && (N % i)){ i++; } if((i * i) > N){ i = N; } count = 1 + (N-i)/2; return count; } int main(){ int N = 10; cout<<"Count of operations of the given type required to reduce N to 0 are: "<<N_to_Zero(N); return 0; }
Output
If we run the above code it will generate the following output −
Count of operations of the given type required to reduce N to 0 are: 5
- Related Articles
- Count the number of operations required to reduce the given number in C++
- Count number of step required to reduce N to 1 by following certain rule in C++
- Find maximum operations to reduce N to 1 in Python
- Find maximum operations to reduce N to 1 in C++
- Count the number of carry operations required to add two numbers in C++
- Reduce a number to 1 by performing given operations in C++
- Program to find number of given operations required to reach Target in Python
- Count of lines required to write the given String in C++
- Program to count number of operations required to all cells into same color in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to count number of operations required to convert all values into same in Python?
- Finding minimum number of required operations to reach n from m in JavaScript
- Program to find number of operations needed to decrease n to 0 in C++
- Minimum number of given operations required to make two strings equal using C++.
- Count of suffix increment/decrement operations to construct a given array in C++
