Number formed after K times repeated addition of smallest divisor of N

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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

18


Time complexity: O(√N )

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.