Tribonacci Numbers | A space optimized based PHP program to print first n Tribinocci numbers . | ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
< ? php function printTrib ( $ n ) { if ( $ n < 1 ) return ; $ first = 0 ; $ second = 0 ; $ third = 1 ; echo $ first , " " ; if ( $ n > 1 ) echo $ second , " ▁ " ; if ( $ n > 2 ) echo $ second , " ▁ " ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) { $ curr = $ first + $ second + $ third ; $ first = $ second ; $ second = $ third ; $ third = $ curr ; echo $ curr , " " ; } } $ n = 10 ; printTrib ( $ n ) ; ? >