tribonacci series c++ code example
Example 1: tribonacci series c++
int n;
cin>>n;
int a=0,b=0,c=1;
if (n < 3) return 0;
cout<<a<<" "<<b<<" "<<c<<" ";
for(int i = 1; i<= n-3; i++){
int d = a + b + c;
cout<<d<<" ";
a = b;
b = c;
c = d;
}
Example 2: iterative tribonacci python
def trib_iter(m):
lista=[m]
c=0
d=True
while d==True:
novalista=[]
for n in lista:
if n in [0,1,2]:
c+=1
else:
for i in range(1,4):
if n-i in [0,1,2]:
c+=1
else:
if n-i<0:
pass
else:
novalista.append(n-i)
if novalista==[]:
d=False
else:
lista=novalista
return c