Back to photostream

N-th Tribonacci Number Leetcode Solution

Problem statement

In the problem " N-th Tribonacci Number" we are given a number n. Our task is to find out the N-th tribonacci number.

 

The zeroth tribonacci number is 0. The first tribonacci number is 1. The second tribonacci number is 1.

 

N-th tribonacci number is summation of (N-1- th tribonacci number), (N-2- th tribonacci number), and (N-3- th tribonacci number).

 

Example

n = 4

4

Explanation: As zeroth, first ,and second tribonacci numbers are 0,1,1 respectively. So the third tribonacci number is (0+1+1) 2. Likewise, the fourth tribonacci is (1+1+2) 4.

Approach for N-th Tribonacci Number Leetcode Solution

As N-th tribonacci number is defined as the summation of (N-1), (N-2), and (N-3) tribonacci number. So we first need the (N-3)-th tribonacci number this will be used in calculating (N-2), (N-1), and (N)-th tribonacci number. So now our new problem is to calculate (N-3)-th tribonacci number. Here we can conclude one thing that is to calculate the N-th tribonacci number we need to calculate one to N-th tribonacci number because every next value is dependent on the previous three values. We will follow these steps:

 

- We will store the values of zeroth, first and second tribonacci numbers in three variables namely a, b, and c respectively.

- Here a,b, and c will store the last three tribonacci numbers. Using these last three tribonacci numbers we will calculate the next tribonacci number and then update the values of a,b, and c.

 

www.tutorialcup.com/leetcode-solutions/n-th-tribonacci-nu...

16 views
0 faves
0 comments
Uploaded on October 14, 2021