Gijswijt 's Sequence | if the sequence is a ( 1 ) a ( 2 ) a ( 3 ) . . a ( n - 1 ) check if the sequence can be represented as x * ( y ^ k ) find the largest value of k ; count ; pattern of elements of size i from the end of sequence ; count ; extract the pattern in a reverse order ; check how many times the pattern is repeated ; if the element dosent match ; if the end of pattern is reached set value of k = 0 and increase the count ; return the max count ; print first n terms of Gijswijt 's sequence ; set the count ; stoes the element ; print the first n terms of the sequence ; push the element ; find the count for next number ; Driver code
function find_count ( ele ) { let count = 0 ; for ( let i = 0 ; i < ele . length ; i ++ ) { let p = [ ] ; let c = 0 ; for ( let j = ele . length - 1 ; j >= ( ele . length - 1 - i ) && j >= 0 ; j -- ) p . push ( ele [ j ] ) ; let j = ele . length - 1 , k = 0 ; while ( j >= 0 ) { if ( ele [ j ] != p [ k ] ) break ; j -- ; k ++ ; if ( k == p . length ) { c ++ ; k = 0 ; } } count = Math . max ( count , c ) ; } return count ; } function solve ( n ) { let count = 1 ; let ele = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( count + " " ) ; ele . push ( count ) ; count = find_count ( ele ) ; } } let n = 10 ; solve ( n ) ;