Given two integers N and K, the task is to generate the final outcome of performing K operations which involves adding the smallest divisor, other than 1, of the current value of N to it at every step.
Example:
Input: N = 9, K = 4
Output: 18
Explanation:
Divisors of 9 are {1, 3, 9}
1st Operation: N = 9 + 3 = 12
2nd Operation: N = 12 + 2 = 14
3rd Operation: N = 14 + 2 = 16
4th Operation: N = 16 + 2 = 18Input: N = 16, K = 3
Output: 22
Naive Approach: The brute force approach for this problem is to perform the operation K times and then print th final number.
Efficient Approach: The trick here is that if the given N is even, smallest divisor will be always 2 for all the K operations. Hence, the required Kth number will be simply
required number = N + K * 2
Also if N is odd, the smallest divisor will be odd. Hence adding them will result in an even value (odd + odd = even). Therefore now the above trick can be applied for (K-1) operations, i.e.,
required number = N + smallest divisor of N + (K – 1) * 2
Below is the implementation of the above approach:
C++
// C++ program to find the Kth number // formed after repeated addition of // smallest divisor of N #include<bits/stdc++.h> #include <cmath> using namespace std; void FindValue(int N, int K) { // If N is even if( N % 2 == 0 ) { N = N + 2 * K; } // If N is odd else { int i; for(i = 2; i < sqrt(N) + 1; i++) { if(N % i == 0) break; } // Add smallest divisor to N N = N + i; // Updated N is even N = N + 2 * ( K - 1 ); } cout << N << endl; } // Driver code int main() { int N = 9; int K = 4; FindValue( N, K ); } // This code is contributed by Surendra_Gangwar |
Java
// Java program to find the Kth number // formed after repeated addition of // smallest divisor of N import java.util.*; class GFG{ static void FindValue(int N, int K) { // If N is even if( N % 2 == 0 ) { N = N + 2 * K; } // If N is odd else { int i; for(i = 2; i < Math.sqrt(N) + 1; i++) { if(N % i == 0) break; } // Add smallest divisor to N N = N + i; // Updated N is even N = N + 2 * ( K - 1 ); } System.out.print(N); } // Driver code public static void main(String args[]) { int N = 9; int K = 4; FindValue( N, K ); } } // This code is contributed by Nidhi_biet |
Python3
# Python3 program to find the # Kth number formed after # repeated addition of # smallest divisor of N import math def FindValue(N, K): # If N is even if( N % 2 == 0 ): N = N + 2 * K # If N is odd else: # Find the smallest divisor for i in range( 2, (int)(math.sqrt(N))+1 ): if( N % i == 0): break # Add smallest divisor to N N = N + i # Updated N is even N = N + 2 * ( K - 1 ) print(N) # Driver code if __name__ == "__main__": N = 9 K = 4 FindValue( N, K ) |
C#
// C# program to find the Kth number // formed after repeated addition of // smallest divisor of N using System; class GFG{ static void FindValue(int N, int K) { // If N is even if( N % 2 == 0 ) { N = N + 2 * K; } // If N is odd else { int i; for(i = 2; i < Math.Sqrt(N) + 1; i++) { if(N % i == 0) break; } // Add smallest divisor to N N = N + i; // Updated N is even N = N + 2 * ( K - 1 ); } Console.WriteLine(N); } // Driver code public static void Main() { int N = 9; int K = 4; FindValue( N, K ); } } // This code is contributed by Code_Mech |
18
Time complexity: O(√N )
Recommended Posts:
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Minimum repeated addition of even divisors of N required to convert N to M
- Minimum cost to convert M to N by repeated addition of its even divisors
- Smallest prime divisor of a number
- Smallest N digit number with none of its digits as its divisor
- Find the k-th smallest divisor of a natural number N
- Recursive sum of digits of a number formed by repeated appends
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Maximum sum after repeatedly dividing N by a divisor
- Add N digits to A such that it is divisible by B after each addition
- Find the maximum component size after addition of each edge to the graph
- String which when repeated exactly K times gives a permutation of S
- Largest element in the array that is repeated exactly k times
- Minimum divisor of a number to make the number perfect cube
- Number formed by adding product of its max and min digit K times
- Find smallest number formed by inverting digits of given number N
- Smallest length string with repeated replacement of two distinct adjacent
- Greatest divisor which divides all natural number in range [L, R]
- Largest Divisor of a Number not divisible by a perfect square
- Largest Divisor for each element in an array other than 1 and the number itself
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.


