-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.js
43 lines (36 loc) · 1020 Bytes
/
fibonacci.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function fibonacci1(n) {
if (n < 2)
return n;
return BigInt(fibonacci1(n - 1)) + BigInt(fibonacci1(n - 2));
}
function fibonacci2(n) {
const result = [0, 1];
for (let i = 2; i <= n; i++) {
result.push(BigInt(result[i - 1]) + BigInt(result[i - 2]));
}
return result[n];
}
function fibonacciMemoize(func) {
const cache = {};
const resultantFunc = function (...args) {
const n = args[0];
if (cache[n])
return cache[n];
const result = func.call(this, n);
cache[n] = result;
return result;
}
Object.defineProperty(resultantFunc, 'name', {value: func.name , writable: false});
return resultantFunc;
}
function fibonacci3(n) {
if (n < 2)
return n;
return BigInt(fibonacci3(n - 1)) + BigInt(fibonacci3(n - 2));
}
fibonacci3 = fibonacciMemoize(fibonacci3);
/* RETURNS Nth TERM OF FIBONACCI SERIES */
/* Args:-
arg1 => n, nth term.
*/
module.exports = { fibonacci1, fibonacci2, fibonacci3 }