text: Golomb Sequence | Set 2 | Javascript program to find the first N terms of Golomb Sequence ; Function to print the Golomb Sequence ; Initialise the array ; Initialise the cnt to 0 ; First and second element of Golomb Sequence is 0 , 1 ; Map to store the count of current element in Golomb Sequence ; Store the count of 2 ; Iterate over 2 to N ; If cnt is equals to 0 then we have new number for Golomb Sequence which is 1 + previous element ; Else the current element is the previous element in this Sequence ; Map the current index to current value in arr [ ] ; Print the Golomb Sequence ; Driver Code
code: var MAX = 100001 ; function printGolombSequence ( N ) { var arr = Array ( MAX ) ; var cnt = 0 ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; var M = new Map ( ) ; M . set ( 2 , 2 ) ; for ( var i = 2 ; i <= N ; i ++ ) { if ( cnt == 0 ) { arr [ i ] = 1 + arr [ i - 1 ] ; cnt = M . get ( arr [ i ] ) ; cnt -- ; } else { arr [ i ] = arr [ i - 1 ] ; cnt -- ; } M . set ( i , arr [ i ] ) ; } for ( var i = 1 ; i <= N ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } var N = 11 ; printGolombSequence ( N ) ;