Find N distinct integers with GCD of sequence as 1 and GCD of each pair greater than 1
Last Updated :
15 Sep, 2022
Given an integer N, the task is to find a sequence of N distinct positive integers such that the Greatest Common Divisor of the sequence is 1 and GCD of all possible pairs of elements is greater than 1.
Input: N = 4
Output: 84 60 105 70
Explanation: The GCD(84, 60, 105, 70) is 1 and the GCD of all possible pair of elements i.e, {(84, 60), (84, 105), (84, 70), (60, 105), (60, 70), (105, 70)} is greater than 1.
Input: N = 3
Output: 6 10 15
Approach: This problem can be solved by using the Set Data Structure. The idea is to choose three integers of the form (a*b, b*c, c*a), as GCD of the three is 1 and pairwise GCD of the three is always greater than 1. Further, simply add the multiples of the three integers until the sequence contains the required number of integers. A set of integers of the form (a*b, b*c, c*a) is (6, 10, 15). Therefore, add multiples of 6, 10, and 15 to the sequence and print the required number of integers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSequence( int N)
{
if (N == 3) {
cout << "6 10 15" << endl;
return ;
}
set< int > s;
s.insert(6);
s.insert(10);
s.insert(15);
for ( int i = 12; i <= 10000; i += 6)
s.insert(i);
for ( int i = 20; i <= 10000; i += 10)
s.insert(i);
for ( int i = 30; i <= 10000; i += 15)
s.insert(i);
int cnt = 0;
for ( int x : s) {
cout << x << " " ;
cnt++;
if (cnt == N) {
break ;
}
}
}
int main()
{
int N = 3;
findSequence(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findSequence( int N)
{
if (N == 3 )
{
System.out.println( "6 10 15" );
return ;
}
Set<Integer> s = new HashSet<Integer>();
s.add( 6 );
s.add( 10 );
s.add( 15 );
for ( int i = 12 ; i <= 10000 ; i += 6 )
s.add(i);
for ( int i = 20 ; i <= 10000 ; i += 10 )
s.add(i);
for ( int i = 30 ; i <= 10000 ; i += 15 )
s.add(i);
int cnt = 0 ;
for (Integer x : s)
{
System.out.print(x + " " );
cnt++;
if (cnt == N)
{
break ;
}
}
}
public static void main(String[] args)
{
int N = 3 ;
findSequence(N);
}
}
|
Python3
def findSequence(N):
if (N = = 3 ):
print ( "6 10 15" )
return
s = set ()
s.add( 6 )
s.add( 10 )
s.add( 15 )
for i in range ( 12 , 10001 , 6 ):
s.add(i)
for i in range ( 20 , 10001 , 10 ):
s.add(i)
for i in range ( 30 , 10001 , 15 ):
s.add(i)
cnt = 0
for x in s:
print (x, end = " " )
cnt + = 1
if (cnt = = N):
break
if __name__ = = "__main__" :
N = 3
findSequence(N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findSequence( int N)
{
if (N == 3) {
Console.WriteLine( "6 10 15" );
return ;
}
HashSet< int > s = new HashSet< int >();
s.Add(6);
s.Add(10);
s.Add(15);
for ( int i = 12; i <= 10000; i += 6)
s.Add(i);
for ( int i = 20; i <= 10000; i += 10)
s.Add(i);
for ( int i = 30; i <= 10000; i += 15)
s.Add(i);
int cnt = 0;
foreach ( int x in s)
{
Console.Write(x + " " );
cnt++;
if (cnt == N) {
break ;
}
}
}
public static void Main(String[] args)
{
int N = 3;
findSequence(N);
}
}
|
Javascript
<script>
function findSequence(N)
{
if (N == 3) {
document.write( "6 10 15" );
return ;
}
let s = new Set();
s.add(6);
s.add(10);
s.add(15);
for (let i = 12; i <= 10000; i += 6)
s.add(i);
for (let i = 20; i <= 10000; i += 10)
s.add(i);
for (let i = 30; i <= 10000; i += 15)
s.add(i);
let cnt = 0;
for (x of s) {
document.write(x + " " );
cnt++;
if (cnt == N) {
break ;
}
}
}
let N = 3;
findSequence(N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...