If we don’t need to solve very large values and are time-sensitive, we can find all values in advance.
This function will work strictly in the case that we’re dealing with \(32\) bit signed integers (which could be a constraint in languages like Java, C/C++, etc.)
The tribonacci sequence grows very quickly, which means that only the first 37 tribonacci numbers fit within the range of a \(32\) bit signed integer.
This method requires only a quick list lookup to find the nth tribonacci number, so it runs in constant time. Since the list is of fixed length, this method runs in constant space as well.
Here shows 5 Approaches to slove this problem: Brute Force use \(\texttt{Long}\) , Brute Force use \(\texttt{Int}\) , Binary Search use \(\texttt{Long}\) , Binary Search use \(\texttt{Int}\) and Recursion.
Intuition
To divide two integers without using multiplication, division, and mod operator, so we can subtract the \(\textit{divisor}\) from the \(\textit{dividend}\) util the \(\textit{result} \ge 0\) .
Brute Force use Long
We can make the \(\textit{dividend}\) and \(\textit{divisor}\) positive, and cast to \(\texttt{long}\), then process.