Given two integers L and R which denotes a range, the task is to find the largest co-prime set of integers in range L to R.
Examples:
Input: L = 10, R = 25
Output: 10 11 13 17 19 21 23Input: L = 45, R = 57
Output: 45 46 47 49 53
Approach: The idea is to iterate from L to R and try to place the integer in a set such that the Greatest common divisor of the set remains 1. This can be done by storing the LCM of the set and each time before adding the element into the set check that the GCD of the number with LCM of the set remains 1. Finally, find the largest such set of the integers.
For Example:
Let L = 10, R = 14
Element 10:
// Co-prime Sets
S = {{10}},
LCM of Co-prime sets
A = {10}
Element 11:
// Element 11 can be added to
// the first set
S = {{10, 11}}
A = {110}
Element 12:
S = {{10, 11}, {12}}
A = {110, 12}
Element 13:
S = {{10, 11, 13}, {12}}
A = {1430, 12}
Element 14:
S = {{10, 11, 13}, {12}, {14}}
A = {1430, 12, 14}
Python
# Python implementation to find # the largest co-prime set in a # given range import math # Function to find the largest # co-prime set of the integers def findCoPrime(n, m): # Initialize sets # with starting integers a =[n] b =[[n]] # Iterate over all the possible # values of the integers for i in range(n + 1, m + 1): # lcm of each list in array # 'b' stored in list 'a' # so go through list 'a' for j in range(len(a)): # if there gcd is 1 then # element add in that # list corresponding to b if math.gcd(i, a[j])== 1: # update the new lcm value q =(i * a[j])//math.gcd(i, a[j]) r = b[j] r.append(i) b[j]= r a[j]= q else: a.append(i) b.append([i]) maxi = [] for i in b: if len(i) > len(maxi): maxi = i print(*maxi) # Driver Code if __name__ == "__main__": n = 10 m = 14 findCoPrime(n, m) |
10 11 13
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of integers up to N which are non divisors and non coprime with N
- Largest number less than or equal to N/2 which is coprime to N
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Composite XOR and Coprime AND
- Coprime divisors of a number
- Print all distinct Coprime sets possible from 1 to N
- Partition first N natural number into two sets such that their sum is not coprime
- Finding a Non Transitive Coprime Triplet in a Range
- Print all Coprime path of a Binary Tree
- Count all pairs of divisors of a number N whose sum is coprime with N
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Median in a stream of integers (running integers)
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- How to sum two integers without using arithmetic operators in C/C++?
- Program to add two integers of given base
- Check if the XOR of an array of integers is Even or Odd
- Armstrong Numbers between two integers
- Sum of last digit of all integers from 1 to N divisible by M
- Find N distinct integers with zero sum
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
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.

