Tribonnaci Sequence

//I need some help to generate tribonnaci sequence starting from 1,1,1,3,5,9
the user can input the sequence max, below is my code in c++
//The problem is the first 3 output won't be 1,1,1
#include <iostream>

using namespace std;

int main(){
int input, a=0, b=0, Total=1, c=1;
cout << "Please input a top number: ";
cin >> input;

for(int i = 1; i <= input; i++)
{


Total = a + b + c ;

a = b;
b = c;
c = Total ;

cout << Total << endl;

}//for
}//main
Not the most elegant perhaps, but "works"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>

typedef unsigned int uint;

void tribonacci(uint seq, uint a = 1, uint b = 1, uint c = 1)
{
  if(seq)
  {
    if(c == 1)
    {
      seq -= 2;
      std::cout << a << ", " << b << ", " << c;
    } else {
      std::cout << ", " << c;
    }
    tribonacci(--seq,b,c,a+b+c);
  } else {
    std::cout << std::endl;
  }
}

int main()
{
  uint seq;
  std::cout << "Enter the number of sequences to extend the tribonacci sequence { 1, 1, 1 }" << std::endl;
  std::cout << ">> "; std::cin >> seq; std::cout << std::endl;
  tribonacci(seq);
  return 0;
}

Last edited on
Topic archived. No new replies allowed.