Open In App

Minimize Y for given N to minimize difference between LCM and GCD

Last Updated : 19 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

 Given an integer N, the task is to find a minimum possible Y that satisfies the following condition:

  • Y > N.
  • Y is not divisible by N (except N = 1).
  • The absolute difference of gcd and lcm of N and Y i.e. | gcd(N, Y)?lcm(N, Y) | is as minimum as possible

Examples:

Input: N = 3 
Output: 4
?Explanation: When  Y = 4 is taken then GCD(3, 4) = 1 and LCM(3, 4) = 12, | 1?12 | = 11 . so, 11 is the smallest value we can get by choosing Y as 4.

Input: N = 4
?Output: 6

Approach: The problem can be solved based on the following idea:

  • First we check N is prime. If it is true return N+1 .
  • Else, find the smallestDivisor of N and print (smallestDivisor+1)*(N/smallestDivisor)

Follow the steps mentioned below to implement the above idea:

  • If N is less than 1, return false.
  • Iterate a loop from (i = 2) to square root of N and check if the number is prime or not. 
    • If yes then return false.
    • Otherwise, return true.
  • If N is prime, then return N+1 as a minimized number Y.
    • Else find the smallest divisor of N.
    • Then return (smallestDivisor+1)*(N/smallestDivisor) as a minimized number Y.

Below is the implementation of the above approach.

C++




// C++ implementation
#include <bits/stdc++.h>
using namespace std;
 
// Function to find smallest divisor
int smallestdivisor(int n)
{
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return i;
    }
  }
  return -1;
}
// Function to check if n is prime or not
bool isPrime(int n)
{
  // Corner case
  if (n < 1)
    return false;
 
  // Check from 2 to square root of n
  for (int i = 2; i <= sqrt(n); i++)
    if (n % i == 0)
      return false;
 
  return true;
}
 
void find(int n)
{
  if (isPrime(n)) {
    cout << (n + 1);
  }
  else {
    int k = smallestdivisor(n);
    cout << ((k + 1) * (n / k));
  }
}
 
int main()
{
  int N = 3;
 
  // Function Call
  find(N);
  //    cout << "GFG!";
  return 0;
}
// this code is contributed by ksam24000


Java




// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find smallest divisor
    static long smallestdivisor(long n)
    {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return i;
            }
        }
        return -1;
    }
 
    // Function to minimize number Y
    public static void find(long n)
    {
        if (isPrime(n)) {
            System.out.println(n + 1);
        }
        else {
            long k = smallestdivisor(n);
            System.out.println((k + 1) * (n / k));
        }
    }
 
    // Function to check if n is prime or not
    static boolean isPrime(long n)
    {
        // Corner case
        if (n < 1)
            return false;
 
        // Check from 2 to square root of n
        for (int i = 2; i <= Math.sqrt(n); i++)
            if (n % i == 0)
                return false;
 
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        long N = 3;
 
        // Function Call
        find(N);
    }
}


Python3




# Python code to implement the approach
 
import math
 
# Function to find smallest divisor
def smallestdivisor(n):
    for i in range(2, int(math.sqrt(n)+1)):
        if(n % i == 0):
            return i
 
    return -1
 
# Function to minimize number Y
def find(n):
    if(isPrime(n)):
        print(n+1)
    else:
        k = smallestdivisor(n)
        print((k + 1) * (n/k))
 
# Function to check if n is prime or not
 
 
def isPrime(n):
    # Corner case
    if(n < 1):
        return False
    # Check from 2 to square root of n
    for i in range(2, int(math.sqrt(n)+1)):
        if(n % i == 0):
            return False
 
    return True
 
 
N = 3
# Function call
find(N)
 
# This code is contributed by lokesh


C#




// C# code to implement the approach
using System;
public class GFG {
 
  // Function to find smallest divisor
  static int smallestdivisor(int n)
  {
    for (int i = 2; i <= Math.Sqrt(n); i++) {
      if (n % i == 0) {
        return i;
      }
    }
    return -1;
  }
 
  // Function to minimize number Y
  public static void find(int n)
  {
    if (isPrime(n)) {
      Console.WriteLine(n + 1);
    }
    else {
      int k = smallestdivisor(n);
      Console.WriteLine((k + 1) * (n / k));
    }
  }
 
  // Function to check if n is prime or not
  static bool isPrime(int n)
  {
    // Corner case
    if (n < 1)
      return false;
 
    // Check from 2 to square root of n
    for (int i = 2; i <= Math.Sqrt(n); i++)
      if (n % i == 0)
        return false;
 
    return true;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int N = 3;
 
    // Function Call
    find(N);
  }
}
 
// This code is contributed by AnkThon


Javascript




   // JavaScript code for the above approach
 
   // Function to find smallest divisor
   function smallestdivisor(n) {
     for (let i = 2; i <= Math.sqrt(n); i++) {
       if (n % i == 0) {
         return i;
       }
     }
     return -1;
   }
 
   // Function to minimize number Y
   function find(n) {
     if (isPrime(n)) {
      console.log(n + 1);
     }
     else {
       let k = smallestdivisor(n);
       console.log((k + 1) * (n / k));
     }
   }
 
   // Function to check if n is prime or not
   function isPrime(n) {
     // Corner case
     if (n < 1)
       return false;
 
     // Check from 2 to square root of n
     for (let i = 2; i <= Math.sqrt(n); i++)
       if (n % i == 0)
         return false;
 
     return true;
   }
 
   // Driver code
 
   let N = 3;
 
   // Function Call
   find(N);
 
// This code is contributed by Potta Lokesh


Output

4

Time Complexity: O(?N) // since there runs a loop from 0 to sqrt(n).
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads