Find nth term of the Dragon Curve Sequence | function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output
< ? php function Dragon_Curve_Sequence ( $ n ) { $ s = "1" ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ temp = "1" ; $ prev = '1' ; $ zero = '0' ; $ one = '1' ; for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) { $ temp . = $ s [ $ j ] ; if ( $ prev == '0' ) { $ temp . = $ one ; $ prev = $ one ; } else { $ temp . = $ zero ; $ prev = $ zero ; } } $ s = $ temp ; } return $ s ; } $ n = 4 ; $ s = Dragon_Curve_Sequence ( $ n ) ; echo $ s . " STRNEWLINE " ; ? >