Print numbers such that no two consecutive numbers are co | C ++ implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the required sequence ; Store only the required primes ; Base condition ; First integer in the list ; Second integer in the list ; Third integer in the list ; Generate ( N - 1 ) th term ; Generate Nth term ; Modify first term ; Print the sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 620000 ; int prime [ MAX ] ; void Sieve ( ) { for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) { prime [ j ] = 1 ; } } } } void printSequence ( int n ) { Sieve ( ) ; vector < int > v , u ; for ( int i = 13 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { v . push_back ( i ) ; } } if ( n == 3 ) { cout << 6 << " ▁ " << 10 << " ▁ " << 15 ; return ; } int k ; for ( k = 0 ; k < n - 2 ; k ++ ) { if ( k % 3 == 0 ) { u . push_back ( v [ k ] * 6 ) ; } else if ( k % 3 == 1 ) { u . push_back ( v [ k ] * 15 ) ; } else { u . push_back ( v [ k ] * 10 ) ; } } k -- ; u . push_back ( v [ k ] * 7 ) ; u . push_back ( 7 * 11 ) ; u [ 0 ] = u [ 0 ] * 11 ; for ( int i = 0 ; i < u . size ( ) ; i ++ ) { cout << u [ i ] << " ▁ " ; } } int main ( ) { int n = 4 ; printSequence ( n ) ; return 0 ; }