Recaman 's sequence | Print first n terms of Recaman sequence ; Prlet first term and store it in a hash ; Prlet remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
function recaman ( n ) { if ( n <= 0 ) return ; document . write ( 0 + " " ) ; let s = new Set ( ) ; s . add ( 0 ) ; let prev = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let curr = prev - i ; if ( curr < 0 || s . has ( curr ) ) curr = prev + i ; s . add ( curr ) ; document . write ( curr + " " ) ; prev = curr ; } } let n = 17 ; recaman ( n ) ;