Find N distinct integers with GCD of sequence as 1 and GCD of each pair greater than 1

13 Dec, 2021
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 = 4Output: 84 60 105 70Explanation: 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 = 3Output: 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++
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to find the sequence of// distinct integers with GCD equal// to 1 and every pair has GCD > 1.void findSequence(int N){    // Special case    if (N == 3) {        cout << "6 10 15" << endl;        return;    }     // Set to avoid duplicates    set<int> s;    s.insert(6);    s.insert(10);    s.insert(15);     // Add multiples of 6    for (int i = 12; i <= 10000; i += 6)        s.insert(i);     // Add multiples of 10    for (int i = 20; i <= 10000; i += 10)        s.insert(i);     // Add multiples of 15    for (int i = 30; i <= 10000; i += 15)        s.insert(i);           int cnt = 0;     // Print first N numbers of set    for (int x : s) {        cout << x << " ";        cnt++;        if (cnt == N) {            break;        }    }} // Driver Codeint main(){    int N = 3;    findSequence(N);     return 0;}
// Java program for above approachimport java.util.*; class GFG{ // Function to find the sequence of// distinct integers with GCD equal// to 1 and every pair has GCD > 1.static void findSequence(int N){         // Special case    if (N == 3)    {        System.out.println("6 10 15");        return;    }     // Set to avoid duplicates    Set<Integer> s = new HashSet<Integer>();    s.add(6);    s.add(10);    s.add(15);     // Add multiples of 6    for(int i = 12; i <= 10000; i += 6)        s.add(i);     // Add multiples of 10    for(int i = 20; i <= 10000; i += 10)        s.add(i);     // Add multiples of 15    for(int i = 30; i <= 10000; i += 15)        s.add(i);     int cnt = 0;     // Print first N numbers of set    for(Integer x : s)    {        System.out.print(x + " ");        cnt++;                 if (cnt == N)        {            break;        }    }} // Driver Codepublic static void main(String[] args){    int N = 3;         findSequence(N);}} // This code is contributed by Potta Lokesh
# python program for above approach # Function to find the sequence of# distinct integers with GCD equal# to 1 and every pair has GCD > 1.def findSequence(N):     # Special case    if (N == 3):        print("6 10 15")        return     # Set to avoid duplicates    s = set()    s.add(6)    s.add(10)    s.add(15)     # Add multiples of 6    for i in range(12, 10001, 6):        s.add(i)     # Add multiples of 10    for i in range(20, 10001, 10):        s.add(i)     # Add multiples of 15    for i in range(30, 10001, 15):        s.add(i)     cnt = 0     # Print first N numbers of set    for x in s:        print(x, end=" ")        cnt += 1        if (cnt == N):            break # Driver Codeif __name__ == "__main__":     N = 3    findSequence(N) # This code is contributed by rakeshsahni
// C# program for above approachusing System;using System.Collections.Generic;class GFG {     // Function to find the sequence of    // distinct integers with GCD equal    // to 1 and every pair has GCD > 1.    static void findSequence(int N)    {         // Special case        if (N == 3) {            Console.WriteLine("6 10 15");            return;        }         // Set to avoid duplicates        HashSet<int> s = new HashSet<int>();        s.Add(6);        s.Add(10);        s.Add(15);         // Add multiples of 6        for (int i = 12; i <= 10000; i += 6)            s.Add(i);         // Add multiples of 10        for (int i = 20; i <= 10000; i += 10)            s.Add(i);         // Add multiples of 15        for (int i = 30; i <= 10000; i += 15)            s.Add(i);         int cnt = 0;         // Print first N numbers of set        foreach(int x in s)        {            Console.Write(x + " ");            cnt++;             if (cnt == N) {                break;            }        }    }     // Driver Code    public static void Main(String[] args)    {        int N = 3;         findSequence(N);    }} // Thiss code is contributed by ukasp.
<script>// Javascript program for above approach // Function to find the sequence of// distinct integers with GCD equal// to 1 and every pair has GCD > 1.function findSequence(N){     // Special case    if (N == 3) {        document.write("6 10 15");        return;    }     // Set to avoid duplicates    let s = new Set();    s.add(6);    s.add(10);    s.add(15);     // Add multiples of 6    for (let i = 12; i <= 10000; i += 6)        s.add(i);     // Add multiples of 10    for (let i = 20; i <= 10000; i += 10)        s.add(i);     // Add multiples of 15    for (let i = 30; i <= 10000; i += 15)        s.add(i);     let cnt = 0;     // Print first N numbers of set    for (x of s) {        document.write(x + " ");        cnt++;        if (cnt == N) {            break;        }    }} // Driver Codelet N = 3;findSequence(N); // This code is contributed by gfgking.</script>
6 10 15
Time Complexity: O(N*log N) Auxiliary Space: O(N)
 
gfgking
lokeshpotta20
rakeshsahni
ukasp
GCD-LCM
Sequence and Series
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Coin Change | DP-7