Modify N by adding its smallest positive divisor exactly K times | Java program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver Code
class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { if ( N % 2 == 0 ) { N += K * 2 ; } else { N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 ; } return N ; } public static void main ( String [ ] args ) { int N = 6 , K = 4 ; System . out . print ( findValOfNWithOperat ( N , K ) ) ; } }