Table of Contents
Problem Statement
The Tribonacci sequence Tn is defined as follows:
T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Given n
, return the value of Tn.
N-th Tribonacci Number JAVA CODE
class Solution {
public int tribonacci(int n) {
if (n<=1){
return n;
}
if (n==2){
return 1;
}
int a=0, b=1, c=1, d=0;
for (int i=3; i<=n; i++){
d = a + b + c;
a = b;
b = c;
c = d;
}
return d;
}
}
This problem is an extension of FIBONACCI NUMBER problem.
Visit Previous Problem#3
Leave a Reply