Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
// Returns true if i is co-prime with numbers 
// in set [2, 3, ... m]
bool isValid(long long int i, long long int m)
{
    // Running the loop till square root of n 
    // to reduce the time complexity from n
    long long int sq_i = sqrt(i);
  
    // Find the minimum of square root of n
    // and m to run the loop until the smaller 
    // one
    long long int sq = min(m, sq_i);
  
    // Check from 2 to min(m, sqrt(n))
    for (long long int j = 2; j <= sq; j++) 
        if (i % j == 0)
            return false;
      
    return true;
}
  
// Function to find the largest number less than n 
// which is Co-prime with all numbers from 2 to m
void findLargestNum(long long int n, long long int m)
{
    // Iterating from n to m+1 to find the number
    for (long long int i = n; i > m; i--) {
  
        // checking every number for the given
        // conditions
        if (isValid(i, m)) {
          
            // The first number which satisfy the 
            // conditions is the answer
            cout << i << '\n';
            return;
        }
    }
  
    // If there is no number which satisfy the 
    // conditions, then print number does not exist.
    cout << "Number Doesn't Exists\n";
}
  
// Driver Program
int main()
{
    long long int n = 16, m = 3;
    findLargestNum(n, m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Largest number in [2, 3, .. n] 
// which is co-prime with numbers 
// in [2, 3, .. m]
import java.io.*;
  
class GFG 
{
    // Returns true if i is co-prime with numbers 
    // in set [2, 3, ... m]
    static boolean isValid(long i, long m)
    {
        // Running the loop till square root of n 
        // to reduce the time complexity from n
        long sq_i = (long)Math.sqrt(i);
      
        // Find the minimum of square root of n
        // and m to run the loop until the smaller 
        // one
        long sq = Math.min(m, sq_i);
      
        // Check from 2 to min(m, sqrt(n))
        for (long j = 2; j <= sq; j++) 
            if (i % j == 0)
                return false;
          
        return true;
    }
      
    // Function to find the largest number less than n 
    // which is Co-prime with all numbers from 2 to m
    static void findLargestNum(long n, long m)
    {
        // Iterating from n to m+1 to find the number
        for (long i = n; i > m; i--) {
      
            // checking every number for the given
            // conditions
            if (isValid(i, m)) {
              
                // The first number which satisfy the 
                // conditions is the answer
                System.out.println (i);
                return;
            }
        }
      
        // If there is no number which satisfy the 
        // conditions, then print number does not exist.
        System.out.println("Number Doesn't Exists");
    }
      
    // Driver Program
    public static void main (String[] args) 
    {
        long n = 16, m = 3;
        findLargestNum(n, m);
              
    }
}
          
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find 
# Largest number in 
# [2, 3, .. n] which is 
# co-prime with 
# numbers in [2, 3, .. m]
import math
  
# Returns true if i is 
# co-prime with numbers 
# in set [2, 3, ... m]
def isValid(i,m) :
      
    # Running the loop 
    # till square root of n 
    # to reduce the time 
    # complexity from n
    sq_i = math.sqrt(i)
  
    # Find the minimum of 
    # square root of n
    # and m to run the loop
    # until the smaller 
    # one
    sq = min(m, sq_i)
  
    # Check from 2 to
    # min(m, sqrt(n))
    for j in range(2, sq + 1) :
        if (i % j == 0) :
            return False
      
    return True
  
# def to find the 
# largest number less 
# than n which is Co-prime  
# with all numbers from 
# 2 to m
def findLargestNum(n, m) :
      
    # Iterating from n to m+1
    # to find the number
    for i in range(n, m, -1) :
  
        # checking every number for 
        # the given conditions
        if (isValid(i, m)) :
          
            # The first number
            # which satisfy the 
            # conditions is the
            # answer
            print ("{}\n".format(i));
            return
  
    # If there is no number 
    # which satisfy the 
    # conditions, then print
    # number does not exist.
    print ("Number Doesn't Exists\n")
  
# Driver Code
n = 16
m = 3
findLargestNum(n, m)
      
# This code is contributed by 
# Manish Shaw(manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Largest number in [2, 3, .. n] 
// which is co-prime with numbers 
// in [2, 3, .. m]
using System;
  
class GFG 
{
    // Returns true if i is co-prime 
    // with numbers in set [2, 3, ... m] 
    static bool isValid(long i, long m)
    {
        // Running the loop till square root
        // of n to reduce the time complexity
        // from n
        long sq_i = (long)Math.Sqrt(i);
      
        // Find the minimum of square root
        // of n and m to run the loop until
        // the smaller one
        long sq = Math.Min(m, sq_i);
      
        // Check from 2 to min(m, sqrt(n))
        for (long j = 2; j <= sq; j++) 
            if (i % j == 0)
                return false;
          
        return true;
    }
      
    // Function to find the largest number
    // less than n which is Co-prime with 
    // all numbers from 2 to m
    static void findLargestNum(long n, long m)
    {
        // Iterating from n to m+1 to find the
        // number
        for (long i = n; i > m; i--) {
      
            // checking every number for the given
            // conditions
            if (isValid(i, m)) {
              
                // The first number which satisfy the 
                // conditions is the answer
                Console.WriteLine(i);
                return;
            }
        }
      
        // If there is no number which satisfy
        // the conditions, then print number does
        // not exist.
        Console.WriteLine("Number Doesn't Exists");
    }
      
    // Driver Program
    public static void Main () 
    {
        long n = 55, m = 25;
        findLargestNum(n, m);
              
    }
}
          
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to find Largest number in  
// [2, 3, .. n] which is co-prime with  
// numbers in [2, 3, .. m]
  
// Returns true if i is 
// co-prime with numbers 
// in set [2, 3, ... m]
function isValid($i,$m)
{
      
    // Running the loop 
    // till square root of n 
    // to reduce the time 
    // complexity from n
    $sq_i = sqrt($i);
  
    // Find the minimum of 
    // square root of n
    // and m to run the loop
    // until the smaller 
    // one
    $sq = min($m, $sq_i);
  
    // Check from 2 to
    // min(m, sqrt(n))
    for ($j = 2; $j <= $sq; $j++) 
        if ($i % $j == 0)
            return false;
      
    return true;
}
  
// Function to find the 
// largest number less than n 
// which is Co-prime with 
// all numbers from 2 to m
function findLargestNum($n, $m)
{
      
    // Iterating from n to m+1
    // to find the number
    for ($i = $n; $i > $m; $i--)
    {
  
        // checking every number for 
        // the given conditions
        if (isValid($i, $m)) 
        {
          
            // The first number
            // which satisfy the 
            // conditions is the
            // answer
            echo $i , "\n";
            return;
        }
    }
  
    // If there is no number 
    // which satisfy the 
    // conditions, then print
    // number does not exist.
    echo "Number Doesn't Exists\n";
}
  
    // Driver Code
    $n = 16; $m = 3;
    findLargestNum($n, $m);
      
// This code is contributed by anuj_67
?>

chevron_right



Output :

13


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, manishshaw1