Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
Last Updated :
08 May, 2023
Given two integers n and m, task is to find a number p which satisfy following conditions :
-> The number p should be less than or equal to n.
-> The number should be co-prime with all the integers from 2 to p(inclusive) i.e. the only positive integer which divides both of the numbers is 1.
Examples :
Input : n = 16, m = 3
Output : 13
Explanation : We need to find largest number
smaller than n and co-prime with all numbers
in set [2, 3, ... m] which is [2, 3] here.
Note that numbers {2, 4, 6, 8, ..} are not
co-prime with 2 and numbers {3, 6, 9, .. }
are not co-prime with 3.
Input : n = 6, m = 5
Output : -1 (Number doesn't exists)
Explanation : In this example 2 will cancel
out 2, 4, 6 and 3 will cancel out 3, 6
and 5 will cancel out 5. No number is left,
so the answer does not exists.
Approach 1 : Create a list of numbers from 2 to n. And then run a loop of i = 2 to m and mark all numbers which are multiples of i. If i is already marked, don’t run loop as its multiples will be already marked. When the loop terminates run a loop from n to 2 until an unmarked number is found. The first unmarked number is the answer, if there is no unmarked number than number doesn’t exists. This approach takes O(n) auxiliary space so if the value of n is too large this method will not work.
Approach 2 : Run a loop from n to p+1 and check every number if it’s not divisible by any number between 2 and m.
C++
#include <bits/stdc++.h>
using namespace std;
bool isValid( long long int i, long long int m)
{
long long int sq_i = sqrt (i);
long long int sq = min(m, sq_i);
for ( long long int j = 2; j <= sq; j++)
if (i % j == 0)
return false ;
return true ;
}
void findLargestNum( long long int n, long long int m)
{
for ( long long int i = n; i > m; i--) {
if (isValid(i, m)) {
cout << i << '\n' ;
return ;
}
}
cout << "Number Doesn't Exists\n" ;
}
int main()
{
long long int n = 16, m = 3;
findLargestNum(n, m);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isValid( long i, long m)
{
long sq_i = ( long )Math.sqrt(i);
long sq = Math.min(m, sq_i);
for ( long j = 2 ; j <= sq; j++)
if (i % j == 0 )
return false ;
return true ;
}
static void findLargestNum( long n, long m)
{
for ( long i = n; i > m; i--) {
if (isValid(i, m)) {
System.out.println (i);
return ;
}
}
System.out.println( "Number Doesn't Exists" );
}
public static void main (String[] args)
{
long n = 16 , m = 3 ;
findLargestNum(n, m);
}
}
|
Python3
import math
def isValid(i,m) :
sq_i = math.sqrt(i)
sq = min (m, sq_i)
for j in range ( 2 , sq + 1 ) :
if (i % j = = 0 ) :
return False
return True
def findLargestNum(n, m) :
for i in range (n, m, - 1 ) :
if (isValid(i, m)) :
print ( "{}\n" . format (i));
return
print ( "Number Doesn't Exists\n" )
n = 16
m = 3
findLargestNum(n, m)
|
C#
using System;
class GFG
{
static bool isValid( long i, long m)
{
long sq_i = ( long )Math.Sqrt(i);
long sq = Math.Min(m, sq_i);
for ( long j = 2; j <= sq; j++)
if (i % j == 0)
return false ;
return true ;
}
static void findLargestNum( long n, long m)
{
for ( long i = n; i > m; i--) {
if (isValid(i, m)) {
Console.WriteLine(i);
return ;
}
}
Console.WriteLine( "Number Doesn't Exists" );
}
public static void Main ()
{
long n = 55, m = 25;
findLargestNum(n, m);
}
}
|
PHP
<?php
function isValid( $i , $m )
{
$sq_i = sqrt( $i );
$sq = min( $m , $sq_i );
for ( $j = 2; $j <= $sq ; $j ++)
if ( $i % $j == 0)
return false;
return true;
}
function findLargestNum( $n , $m )
{
for ( $i = $n ; $i > $m ; $i --)
{
if (isValid( $i , $m ))
{
echo $i , "\n" ;
return ;
}
}
echo "Number Doesn't Exists\n" ;
}
$n = 16; $m = 3;
findLargestNum( $n , $m );
?>
|
Javascript
<script>
function isValid(i, m)
{
let sq_i = Math.sqrt(i);
let sq = Math.min(m, sq_i);
for (let j = 2; j <= sq; j++)
if (i % j == 0)
return false ;
return true ;
}
function findLargestNum(n, m)
{
for (let i = n; i > m; i--) {
if (isValid(i, m)) {
document.write (i);
return ;
}
}
document.write( "Number Doesn't Exists" );
}
let n = 16, m = 3;
findLargestNum(n, m);
</script>
|
Output :
13
Time Complexity: O((n-m)*sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...