Tribonacci Numbers | A space optimized based CPP 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
#include <iostream> NEW_LINE using namespace std ; void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; cout << first << " ▁ " ; if ( n > 1 ) cout << second << " ▁ " ; if ( n > 2 ) cout << second << " ▁ " ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; cout << curr << " ▁ " ; } } int main ( ) { int n = 10 ; printTrib ( n ) ; return 0 ; }