Skip to main content
Back to problems
Leetcode
Easy
Dynamic Programming
Recursion
Math
N Th Tribonacci Number

Compute the nn-th Tribonacci number, where each term is the sum of the previous three terms.

Acceptance 0%
Problem Statement

Problem

The Tribonacci sequence is defined similarly to Fibonacci, but each term depends on the previous three terms.

Let:

  • T0=0T_0 = 0
  • T1=1T_1 = 1
  • T2=1T_2 = 1
  • For n3n \ge 3, Tn=Tn1+Tn2+Tn3T_n = T_{n-1} + T_{n-2} + T_{n-3}

Given an integer nn, return the nn-th Tribonacci number.

Goal

Write a function that computes the value of TnT_n efficiently.

Input Format

  • One integer nn

Output Format

  • Return a single integer: the nn-th Tribonacci number

Constraints

  • 0n370 \le n \le 37
  • The result fits in a 32-bit signed integer
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 4

Output

4

Explanation

The sequence starts 0, 1, 1, 2, 4, ... so T4=4T_4 = 4.

Example 2

Input

n = 25

Output

1389537

Explanation

Using the recurrence repeatedly gives T25=1389537T_{25} = 1389537.

Show 1 more example

Example 3

Input

n = 0

Output

0

Explanation

By definition, T0=0T_0 = 0.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.