Recaman 's sequence | Prints first n terms of Recaman sequence ; Print first term and store it in a hash ; Print remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
< ? php function recaman ( $ n ) { if ( $ n <= 0 ) return ; print ( "0 , ▁ " ) ; $ s = array ( ) ; array_push ( $ s , 0 ) ; $ prev = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr = $ prev - $ i ; if ( $ curr < 0 or in_array ( $ curr , $ s ) ) $ curr = $ prev + $ i ; array_push ( $ s , $ curr ) ; print ( $ curr . " , " ) ; $ prev = $ curr ; } } $ n = 17 ; recaman ( $ n ) ; ? >