Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime

Given an integer N, the task is to print N integers ≤ 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime.

Examples:

Input: N = 3
Output: 6 15 10

Input: N = 4
Output: 6 15 35 14



Approach:

  • We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do this so that the (n – 1)th number, nth and 1st number can also follow the property mentioned in the problem statement.
  • Another important part of the problem is the fact that the numbers should be ≤ 109. If you just multiply consecutive prime numbers, after around 3700 numbers the value will cross 109. So we need to only use those prime numbers whose product wont cross 109.
  • To do this in an efficient way, consider a small number of primes say the first 550 primes and select them in a way such that on making a product no number gets repeated. We first choose every prime consecutively and then choose the primes with an interval of 2 and then 3 and so on, doing that we already make sure that no number gets repeated.

So we will select
5, 11, 17, …
Next time, we can start with 7 and select,
7, 13, 19, …

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
#define limit 1000000000
#define MAX_PRIME 2000000
#define MAX 1000000
#define I_MAX 50000
  
map<int, int> mp;
  
int b[MAX];
int p[MAX];
int j = 0;
bool prime[MAX_PRIME + 1];
  
// Function to generate Sieve of
// Eratosthenes
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p <= n; p++) {
  
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Add the prime numbers to the array b
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            b[j++] = p;
        }
    }
}
  
// Function to return the gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to print the required
// sequence of integers
void printSeries(int n)
{
    SieveOfEratosthenes(MAX_PRIME);
  
    int i, g, k, l, m, d;
    int ar[I_MAX + 2];
  
    for (i = 0; i < j; i++) {
        if ((b[i] * b[i + 1]) > limit)
            break;
  
        // Including the primes in a series
        // of primes which will be later
        // multiplied
        p[i] = b[i];
  
        // This is done to mark a product
        // as existing
        mp[b[i] * b[i + 1]] = 1;
    }
  
    // Maximum number of primes that we consider
    d = 550;
    bool flag = false;
  
    // For different interval
    for (k = 2; (k < d - 1) && !flag; k++) {
  
        // For different starting index of jump
        for (m = 2; (m < d) && !flag; m++) {
  
            // For storing the numbers
            for (l = m + k; l < d; l += k) {
  
                // Checking for occurrence of a
                // product. Also checking for the
                // same prime occurring consecutively
                if (((b[l] * b[l + k]) < limit)
                    && (l + k) < d && p[i - 1] != b[l + k]
                    && p[i - 1] != b[l] && mp[b[l] * b[l + k]] != 1) {
                    if (mp[p[i - 1] * b[l]] != 1) {
  
                        // Including the primes in a
                        // series of primes which will
                        // be later multiplied
                        p[i] = b[l];
                        mp[p[i - 1] * b[l]] = 1;
                        i++;
                    }
                }
  
                if (i >= I_MAX) {
                    flag = true;
                    break;
                }
            }
        }
    }
  
    for (i = 0; i < n; i++)
        ar[i] = p[i] * p[i + 1];
  
    for (i = 0; i < n - 1; i++)
        cout << ar[i] << " ";
  
    g = gcd(ar[n - 1], ar[n - 2]);
    cout << g * 2 << endl;
}
  
// Driver Code
int main()
{
    int n = 4;
  
    printSeries(n);
  
    return 0;
}

chevron_right


Output:

6 15 35 14


My Personal Notes arrow_drop_up


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.