Largest Coprime Set Between two integers
Last Updated :
09 Mar, 2023
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 23
Input: 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}
C++
#include <bits/stdc++.h>
using namespace std;
void findCoPrime( int n, int m)
{
vector< int > a = { n };
vector<vector< int > > b = { a };
for ( int i = n + 1; i <= m; i++) {
for ( int j = 0; j < a.size(); j++) {
if (__gcd(i, a[j]) == 1) {
int q = (i * a[j]) / __gcd(i, a[j]);
b[j].push_back(i);
a[j] = q;
}
}
a.push_back(i);
b.push_back({ i });
}
vector< int > maxi = {};
for ( int i = 0; i < b.size(); i++) {
if (b[i].size() > maxi.size()) {
maxi = b[i];
}
}
for ( auto i = maxi.begin(); i != maxi.end(); i++) {
cout << *i << " " ;
}
cout << endl;
}
int main()
{
int n = 10;
int m = 14;
findCoPrime(n, m);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void findCoPrime( int n, int m)
{
List<Integer> a = new ArrayList<>();
List<List<Integer> > b = new ArrayList<>();
a.add(n);
List<Integer> innerList = new ArrayList<>();
innerList.add(n);
b.add(innerList);
for ( int i = n + 1 ; i <= m; i++) {
for ( int j = 0 ; j < a.size(); j++) {
if (gcd(i, a.get(j)) == 1 ) {
int q = (i * a.get(j)) / gcd(i, a.get(j));
b.get(j).add(i);
a.set(j, q);
}
}
a.add(i);
innerList = new ArrayList<>();
innerList.add(i);
b.add(innerList);
}
List<Integer> maxi = new ArrayList<>();
for (List<Integer> list : b) {
if (list.size() > maxi.size()) {
maxi = list;
}
}
for (Integer num : maxi) {
System.out.print(num + " " );
}
System.out.println();
}
public static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static void main(String[] args)
{
int n = 10 ;
int m = 14 ;
findCoPrime(n, m);
}
}
|
Python
import math
def findCoPrime(n, m):
a = [n]
b = [[n]]
for i in range (n + 1 , m + 1 ):
for j in range ( len (a)):
if math.gcd(i, a[j]) = = 1 :
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)
if __name__ = = "__main__" :
n = 10
m = 14
findCoPrime(n, m)
|
C#
using System;
using System.Collections.Generic;
class MainClass {
public static void findCoPrime( int n, int m) {
List< int > a = new List< int >();
List<List< int >> b = new List<List< int >>();
a.Add(n);
List< int > innerList = new List< int >();
innerList.Add(n);
b.Add(innerList);
for ( int i = n + 1; i <= m; i++) {
for ( int j = 0; j < a.Count; j++) {
if (gcd(i, a[j]) == 1) {
int q = (i * a[j]) / gcd(i, a[j]);
b[j].Add(i);
a[j] = q;
}
}
innerList = new List< int >();
innerList.Add(i);
b.Add(innerList);
a.Add(i);
}
List< int > maxi = new List< int >();
foreach (List< int > list in b) {
if (list.Count > maxi.Count) {
maxi = list;
}
}
foreach ( int num in maxi) {
Console.Write(num + " " );
}
Console.WriteLine();
}
public static int gcd( int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void Main( string [] args) {
int n = 10;
int m = 14;
findCoPrime(n, m);
}
}
|
Javascript
function findCoPrime(n, m) {
let a = [n];
let b = [[n]];
for (let i = n + 1; i <= m; i++) {
for (let j = 0; j < a.length; j++) {
if (gcd(i, a[j]) === 1) {
let q = (i * a[j]) / gcd(i, a[j]);
let r = b[j];
r.push(i);
b[j] = r;
a[j] = q;
}
}
if (!a.includes(i)) {
a.push(i);
b.push([i]);
}
}
let maxi = [];
for (let i = 0; i < b.length; i++) {
if (b[i].length > maxi.length) {
maxi = b[i];
}
}
console.log(...maxi);
}
function gcd(a, b) {
if (b === 0) return a;
return gcd(b, a % b);
}
let n = 10;
let m = 14;
findCoPrime(n, m);
|
Share your thoughts in the comments
Please Login to comment...