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


Co-prime numbers are numbers that do not have any common divisor other than 1. We will be given two numbers n and m. We have to find the largest number in the range of 2 to n (both inclusive) which is co-prime with all the elements in the range of 2 to m (both inclusive). If none of the elements in the given range is co-prime with all the elements of the second range then we have to return or print -1. We will implement the approach, and code, and will discuss the time and space complexity of the program.

Input

N = 20, M = 13 

Output

19

Explanation

We have a total of elements from 2 to 20.

All the even elements have the minimum divisor 2 that means 2, 4, 6, 8, …., 20 are already out.

All the elements which are divisible by the prime numbers in the range of 2 to M are out.

Only left elements are 17 and 19. So, 19 is the result.

Input

N = 10, M = 7

Output

-1

Explanation

As previously discussed all the elements in the range 2 to 10 that are even are eliminated. All the elements that are divided by 3, 5, and 7 are out. This means none of the elements in the range of 2 to 10 are co-prime with the elements in the range of 2 to 7.

Approach 1

In this approach, we will find all the prime numbers or the numbers that are not divisible by any prime number less than equal to m and that are greater than the m. We will use the concept of the sieve of Eratosthenes here but in a modified way to find the number that is not divisible by the prime numbers less than m.

Example

#include <iostream>
using namespace std;
// function to check if any number in ranges are co-prime 
int find(int n, int m){
   // creating array to store the result 
   int arr[n+1] = {0}; 
   for(int i = 2; i <= m; i++){
      // if the current number is not prime then continue
      if(arr[i] == 1){
         continue;
      }
      // current number is prime number mark all the numbers that are not prime and divible by current number
      for(int j = 2; i*j <= n; j++){
         arr[i*j] = 1;
      }
   }
   // finding the last number which is greater than m and is prime
   for(int i = n; i>m; i--){
      if(arr[i] == 0){
         return i;
      }
   }
   return -1;
}
int main(){
   int n = 20;
   int m = 13;
   // calling the function to print the largest element 
   cout<<"The largest number that is co-prime is: " << find(n,m)<<endl;
   return 0;
}

Output

The largest number that is co-prime is: 19

Time and Space Complexity

The time complexity of the above code is equivalent to the sieve of Eratosthenes that is O(M*log(log(M))).

The space complexity of the above code is O(N), where N is the first range.

Approach 2

In this approach, we will run a for loop from the n to the m (n inclusive and m exclusive) and for each number we will check if they are divisible by any number from 2 to m.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check if the current number is divisible by any number in range 
bool isDivisble(int cur, int m){
   // getting square root of the current number 
   int sqr = sqrt(cur);
   // getting minimum of m and sqrt
   sqr = min(sqr,m);
   // checking divisblity 
   for(int i =2; i<= sqr; i++){
      if(cur%i == 0){
         return true;
      }
   }
   return false;
}
// function to check if any number in ranges are co-prime 
int find(int n, int m){
   // traversing over the given range from n to m 
   for(int i =n; i>m; i--){
      if(isDivisble(i, m)){
         continue;
      }
      else{
         return i;
      }
   }
   return -1;
}
int main(){
   int n = 10;
   int m = 7;
   // calling the function to print the largest element 
   cout<<"The largest number that is co-prime is: " << find(n,m)<<endl;
   return 0;
}

Output

The largest number that is co-prime is: -1

Time and Space Complexity

The time complexity of the above code is equal to O(N*sqrt(N)), where sqrt(N) is the square root of the N.

The space complexity of the above code is O(1), as we are not using any extra space here.

From both the above given programs, both are good by their own specifications. The first code takes less time but has high space complexity but the second code takes more time but has non-space complexity.

If the value of N is high then the second code is best otherwise the first one is great.

If there are range queries for fixed m over n, then the first code is best.

Conclusion

In this tutorial, we have implemented a program to find the highest number from the first n element that is co-prime with all the first m elements where 1 is exclusive in both ranges. Co-prime numbers are numbers that do not have any common divisor other than 1. We have implemented an approach that is a modified version of the sieve of Eratosthenes with a time complexity of O(M*log(log(M))) and space complexity of O(N) and another approach with constant space complexity.

Updated on: 01-Sep-2023

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements