Open In App

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

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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 Code
int main()
{
    int N = 3;
    findSequence(N);
 
    return 0;
}


Java




// Java program for above approach
import 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 Code
public static void main(String[] args)
{
    int N = 3;
     
    findSequence(N);
}
}
 
// This code is contributed by Potta Lokesh


Python3




# 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 Code
if __name__ == "__main__":
 
    N = 3
    findSequence(N)
 
# This code is contributed by rakeshsahni


C#




// C# program for above approach
using 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);
    }
}
 
// This code is contributed by ukasp.


Javascript




<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 Code
let N = 3;
findSequence(N);
 
// This code is contributed by gfgking.
</script>


Output

6 10 15

Time Complexity: O(N*log N) 
Auxiliary Space: O(N)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads