Tribonacci Numbers | A space optimized based Javascript program to print first n Tribonacci 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
function printTrib ( n ) { if ( n < 1 ) return ; let first = 0 , second = 0 ; let third = 1 ; document . write ( first + " " ) ; if ( n > 1 ) document . write ( second + " " ) ; if ( n > 2 ) document . write ( second + " " ) ; for ( let i = 3 ; i < n ; i ++ ) { let curr = first + second + third ; first = second ; second = third ; third = curr ; document . write ( curr + " " ) ; } } let n = 10 ; printTrib ( n ) ;