Modify N by adding its smallest positive divisor exactly K times
Last Updated :
11 Oct, 2021
Given two positive integers N and K, the task is to find the value of N after incrementing the value of N in each operation by its smallest divisor exceeding N ( exceeding 1 ), exactly K times.
Examples:
Input: N = 5, K = 2
Output: 12
Explanation:
Smallest divisor of N (= 5) is 5. Therefore, N = 5 + 5 = 10.
Smallest divisor of N (= 10) is 2. Therefore, N = 5 + 2 = 12.
Therefore, the required output is 12.
Input: N = 6, K = 4
Output: 14
Naive Approach: The simplest approach to solve this problem to iterate over the range [1, K] using variable i and in each operation, find the smallest divisors greater than 1 of N and increment the value of N by the smallest divisor greater than 1 of N. Finally, print the value of N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestDivisorGr1( int N)
{
for ( int i = 2; i <= sqrt (N);
i++) {
if (N % i == 0) {
return i;
}
}
return N;
}
int findValOfNWithOperat( int N, int K)
{
for ( int i = 1; i <= K; i++) {
N += smallestDivisorGr1(N);
}
return N;
}
int main()
{
int N = 6, K = 4;
cout << findValOfNWithOperat(N, K);
return 0;
}
|
Java
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)
{
for ( int i = 1 ; i <= K; i++)
{
N += smallestDivisorGr1(N);
}
return N;
}
public static void main(String[] args)
{
int N = 6 , K = 4 ;
System.out.print(findValOfNWithOperat(N, K));
}
}
|
Python3
import math
def smallestDivisorGr1(N):
for i in range ( 2 , int (math.sqrt(N)) + 1 ):
if (N % i = = 0 ):
return i
return N
def findValOfNWithOperat(N, K):
for i in range ( 1 , K + 1 ):
N + = smallestDivisorGr1(N)
return N
if __name__ = = "__main__" :
N = 6
K = 4
print (findValOfNWithOperat(N, K))
|
C#
using System;
public 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)
{
for ( int i = 1; i <= K; i++)
{
N += smallestDivisorGr1(N);
}
return N;
}
public static void Main(String[] args)
{
int N = 6, K = 4;
Console.Write(findValOfNWithOperat(N, K));
}
}
|
Javascript
<script>
function smallestDivisorGr1(N)
{
for (let i = 2; i <= Math.sqrt(N);
i++) {
if (N % i == 0)
{
return i;
}
}
return N;
}
function findValOfNWithOperat(N, K)
{
for (let i = 1; i <= K; i++)
{
N += smallestDivisorGr1(N);
}
return N;
}
let N = 6, K = 4;
document.write(findValOfNWithOperat(N, K));
</script>
|
Time Complexity: O(K * ?N)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to solve the problem:
- If N is an even number then update the value of N to (N + K * 2).
- Otherwise, find the smallest positive divisor greater than 1 of N say, smDiv and update the value N to (N + smDiv + (K – 1) * 2)
- Finally, print the value of N.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int smallestDivisorGr1( int N)
{
for ( int i = 2; i <= sqrt (N);
i++) {
if (N % i == 0) {
return i;
}
}
return N;
}
int findValOfNWithOperat( int N, int K)
{
if (N % 2 == 0) {
N += K * 2;
}
else {
N += smallestDivisorGr1(N)
+ (K - 1) * 2;
}
return N;
}
int main()
{
int N = 6, K = 4;
cout << findValOfNWithOperat(N, K);
return 0;
}
|
Java
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));
}
}
|
Python3
def smallestDivisorGr1(N):
for i in range (sqrt(N)):
i + = 1
if (N % i = = 0 ):
return i
return N
def findValOfNWithOperat(N, K):
if (N % 2 = = 0 ):
N + = K * 2
else :
N + = smallestDivisorGr1(N) + (K - 1 ) * 2
return N
N = 6
K = 4
print (findValOfNWithOperat(N, K))
|
C#
using System;
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;
}
static public void Main()
{
int N = 6, K = 4;
Console.Write(findValOfNWithOperat(N, K));
}
}
|
Javascript
<script>
function smallestDivisorGr1( N)
{
for ( var i = 2; i <= Math.sqrt(N);
i++) {
if (N % i == 0) {
return i;
}
}
return N;
}
function findValOfNWithOperat(N, K)
{
if (N % 2 == 0) {
N += K * 2;
}
else {
N += smallestDivisorGr1(N)
+ (K - 1) * 2;
}
return N;
}
var N = 6, K = 4;
document.write(findValOfNWithOperat(N, K));
</script>
|
Time Complexity: O(?N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...