diff --git a/src/algorithms/math/tribonacci/README.md b/src/algorithms/math/tribonacci/README.md new file mode 100644 index 00000000..3796f91c --- /dev/null +++ b/src/algorithms/math/tribonacci/README.md @@ -0,0 +1,10 @@ +# Tribonacci Number + +The tribonacci numbers are a homogeneous linear recurrence with constant coefficients of order 3 with signature (0, 0, 1), inspired from the Fibonacci numbers (which are of order 2 with signature (0, 1)), i.e. + +`0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136 ...` + +## References + +- [Brilliant](https://brilliant.org/wiki/tribonacci-sequence/) +- [Oeis](http://oeis.org/wiki/Tribonacci_numbers) diff --git a/src/algorithms/math/tribonacci/__test__/tribonacci.test.js b/src/algorithms/math/tribonacci/__test__/tribonacci.test.js new file mode 100644 index 00000000..24bd87f0 --- /dev/null +++ b/src/algorithms/math/tribonacci/__test__/tribonacci.test.js @@ -0,0 +1,16 @@ +import tribonacci from '../tribonacci'; + +describe('tribonacci', () => { + it('should calculate tribonacci correctly', () => { + expect(tribonacci(1)).toEqual([0]); + expect(tribonacci(2)).toEqual([0, 0]); + expect(tribonacci(3)).toEqual([0, 0, 1]); + expect(tribonacci(4)).toEqual([0, 0, 1, 1]); + expect(tribonacci(5)).toEqual([0, 0, 1, 1, 2]); + expect(tribonacci(6)).toEqual([0, 0, 1, 1, 2, 4]); + expect(tribonacci(7)).toEqual([0, 0, 1, 1, 2, 4, 7]); + expect(tribonacci(8)).toEqual([0, 0, 1, 1, 2, 4, 7, 13]); + expect(tribonacci(9)).toEqual([0, 0, 1, 1, 2, 4, 7, 13, 24]); + expect(tribonacci(10)).toEqual([0, 0, 1, 1, 2, 4, 7, 13, 24, 44]); + }); +}); diff --git a/src/algorithms/math/tribonacci/__test__/tribonacciNth.test.js b/src/algorithms/math/tribonacci/__test__/tribonacciNth.test.js new file mode 100644 index 00000000..3f7cec7a --- /dev/null +++ b/src/algorithms/math/tribonacci/__test__/tribonacciNth.test.js @@ -0,0 +1,17 @@ +import tribonacciNth from '../tribonacciNth'; + +describe('tribonacciNth', () => { + it('should calculate tribonacci correctly', () => { + expect(tribonacciNth(1)).toBe(0); + expect(tribonacciNth(2)).toBe(1); + expect(tribonacciNth(3)).toBe(1); + expect(tribonacciNth(4)).toBe(2); + expect(tribonacciNth(5)).toBe(4); + expect(tribonacciNth(6)).toBe(7); + expect(tribonacciNth(7)).toBe(13); + expect(tribonacciNth(8)).toBe(24); + expect(tribonacciNth(20)).toBe(35890); + expect(tribonacciNth(30)).toBe(15902591); + expect(tribonacciNth(50)).toBe(3122171529233); + }); +}); diff --git a/src/algorithms/math/tribonacci/tribonacci.js b/src/algorithms/math/tribonacci/tribonacci.js new file mode 100644 index 00000000..100949ed --- /dev/null +++ b/src/algorithms/math/tribonacci/tribonacci.js @@ -0,0 +1,22 @@ +/** + * Return a tribonacci sequence as an array. + * + * @param n + * @return {number[]} + */ +export default function tribonacci(n) { + const tribSequence = [0]; + + if (n >= 2) { + tribSequence.push(0); + } + + let currentValue = 1; + + for (let i = 2; i < n; i += 1) { + tribSequence.push(currentValue); + currentValue += tribSequence[i - 1] + tribSequence[i - 2]; + } + + return tribSequence; +} diff --git a/src/algorithms/math/tribonacci/tribonacciNth.js b/src/algorithms/math/tribonacci/tribonacciNth.js new file mode 100644 index 00000000..14c5f216 --- /dev/null +++ b/src/algorithms/math/tribonacci/tribonacciNth.js @@ -0,0 +1,17 @@ +/** + * Calculate tribonacci number at specific position using Dynamic Programming approach. + * + * @param n + * @return {number} + */ +export default function tribonacciNth(n) { + const tribSequence = [0, 0, 1]; + let currentValue = 1; + + for (let i = 3; i <= n; i += 1) { + tribSequence.push(currentValue); + currentValue += tribSequence[i - 1] + tribSequence[i - 2]; + } + + return tribSequence[n]; +}