Minimize Y for given N to minimize difference between LCM and GCD
Last Updated :
19 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int smallestdivisor( int n)
{
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0) {
return i;
}
}
return -1;
}
bool isPrime( int n)
{
if (n < 1)
return false ;
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;
find(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static long smallestdivisor( long n)
{
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
if (n % i == 0 ) {
return i;
}
}
return - 1 ;
}
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));
}
}
static boolean isPrime( long n)
{
if (n < 1 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
if (n % i == 0 )
return false ;
return true ;
}
public static void main(String[] args)
{
long N = 3 ;
find(N);
}
}
|
Python3
import math
def smallestdivisor(n):
for i in range ( 2 , int (math.sqrt(n) + 1 )):
if (n % i = = 0 ):
return i
return - 1
def find(n):
if (isPrime(n)):
print (n + 1 )
else :
k = smallestdivisor(n)
print ((k + 1 ) * (n / k))
def isPrime(n):
if (n < 1 ):
return False
for i in range ( 2 , int (math.sqrt(n) + 1 )):
if (n % i = = 0 ):
return False
return True
N = 3
find(N)
|
C#
using System;
public class GFG {
static int smallestdivisor( int n)
{
for ( int i = 2; i <= Math.Sqrt(n); i++) {
if (n % i == 0) {
return i;
}
}
return -1;
}
public static void find( int n)
{
if (isPrime(n)) {
Console.WriteLine(n + 1);
}
else {
int k = smallestdivisor(n);
Console.WriteLine((k + 1) * (n / k));
}
}
static bool isPrime( int n)
{
if (n < 1)
return false ;
for ( int i = 2; i <= Math.Sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
public static void Main( string [] args)
{
int N = 3;
find(N);
}
}
|
Javascript
function smallestdivisor(n) {
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return i;
}
}
return -1;
}
function find(n) {
if (isPrime(n)) {
console.log(n + 1);
}
else {
let k = smallestdivisor(n);
console.log((k + 1) * (n / k));
}
}
function isPrime(n) {
if (n < 1)
return false ;
for (let i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
let N = 3;
find(N);
|
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
Share your thoughts in the comments
Please Login to comment...