N-th Tribonacci Number

Try to solve the N-th Tribonacci Number problem.

Statement#

Given a number n, calculate the corresponding Tribonacci number. The Tribonacci sequence TnT_n is defined as:

T0=0, T1=1, T2=1T_0 = 0,\space T_1 = 1,\space T_2 = 1, and  Tn+3=Tn+Tn+1+Tn+2, \space T_{n+3} = T_n + T_{n+1} + T_{n+2},\space for n>=0n >= 0

The input number, n, is a non-negative integer.

Constraints:

  • 00 \leq n 37\leq 37
  • The answer is guaranteed to fit within a 32-bit integer, i.e., answer 2311\leq 2 ^{31} - 1

Examples#

Created with Fabric.js 3.6.6 Output Input 0 1 1 2 4 7 13 24 Sample example 1 n = 7 24 0 1 1 2 4 7 13 x 0 1 1 2 4 7 13 x

1 of 2

Created with Fabric.js 3.6.6 Output Input 0 1 1 2 4 7 13 24 44 81 149 Sample example 2 n = 10 149 0 1 1 2 4 7 13 24 44 81 x 0 1 1 2 4 7 13 24 44 81 x

2 of 2

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

N-th Tribonacci Number

1

What is the 5th Tribonacci number?

A)

5

B)

7

C)

15

D)

4

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Initialize the first three numbers as 0, 1, and 1 respectively.

If n is less than 3, then the result will be determined by the base case.

Else continue computing the third and next numbers by adding the previous three numbers. Update them until the required number is obtained.


Try it yourself#

Implement your solution in the following coding playground:

Python
usercode > main.py
Input #1
Nth Tribonacci Number

Dynamic Programming: Introduction

Solution: N-th Tribonacci Number