Print numbers such that no two consecutive numbers are co | Javascript 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
let MAX = 620000 ; let prime = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { prime [ i ] = 0 ; } function Sieve ( ) { for ( let i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( let j = 2 * i ; j < MAX ; j += i ) { prime [ j ] = 1 ; } } } } function printSequence ( n ) { Sieve ( ) ; let v = [ ] ; let u = [ ] ; for ( let i = 13 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { v . push ( i ) ; } } if ( n == 3 ) { document . write ( 6 + " " + 10 + " " + 15 ) ; return ; } let k ; for ( k = 0 ; k < n - 2 ; k ++ ) { if ( k % 3 == 0 ) { u . push ( v [ k ] * 6 ) ; } else if ( k % 3 == 1 ) { u . push ( v [ k ] * 15 ) ; } else { u . push ( v [ k ] * 10 ) ; } } k -- ; u . push ( v [ k ] * 7 ) ; u . push ( 7 * 11 ) ; u [ 0 ] = u [ 0 ] * 11 ; for ( let i = 0 ; i < u . length ; i ++ ) { document . write ( u [ i ] + " " ) ; } } let n = 4 ; printSequence ( n ) ;