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 = 18

Input: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 K

^{th}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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ) ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

18

**Time complexity: **O(√N )

## Recommended Posts:

- 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
- Number formed by adding product of its max and min digit K times
- Find smallest number formed by inverting digits of given number N
- Find Nth smallest number that is divisible by 100 exactly K times
- Length of the smallest number which is divisible by K and formed by using 1's only
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- String which when repeated exactly K times gives a permutation of S
- Largest element in the array that is repeated exactly k times
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- GCD of two numbers formed by n repeating x and y times
- Smallest length string with repeated replacement of two distinct adjacent
- Smallest square formed with given rectangles
- Smallest multiple of N formed using the given set of digits
- Minimum divisor of a number to make the number perfect cube
- Smallest integer greater than n such that it consists of digit m exactly k times
- Lexicographically smallest string formed by removing at most one character
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number

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.