Recaman 's sequence | Prints first n terms of Recaman sequence ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver Code
< ? php function recaman ( $ n ) { $ arr [ 0 ] = 0 ; echo $ arr [ 0 ] , " , ▁ " ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr = $ arr [ $ i - 1 ] - $ i ; $ j ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( ( $ arr [ $ j ] == $ curr ) $ curr < 0 ) { $ curr = $ arr [ $ i - 1 ] + $ i ; break ; } } $ arr [ $ i ] = $ curr ; echo $ arr [ $ i ] , " , ▁ " ; } } $ n = 17 ; recaman ( $ n ) ; ? >