Connell Sequence | Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Method
function gen ( n , r ) { var a = r [ r . length - 1 ] ; a ++ ; for ( var i = 1 ; i <= n ; a += 2 , i ++ ) r . push ( a ) ; return r ; } function conell ( n ) { var res = [ ] ; var k = 1 ; res . push ( 0 ) ; while ( 1 ) { res = gen ( k , res ) ; k ++ ; var j = res . length - 1 ; while ( j != n && j + k > n ) k -- ; if ( j >= n ) break ; } res . shift ( ) ; return res ; } var n = 10 ; document . write ( " " + n + " " + " " ) ; var res = conell ( n ) ; for ( var i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ;