forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (58 loc) · 1.79 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
// the algorithm has time complexity of O(n^2), very bad!
function fibonacci(position) {
// if position is 1 or 2, the number in fibonacci sequence will be 1
if (position === 1 || position === 0) {
return position;
} else if (position < 0) {
throw new Error('Invalid Position');
}
// else the element in fibonacci sequence will be the sum of
// element at position(p) (p -1) and (p - 2)
return fibonacci(position - 2) + fibonacci(position - 1);
}
/**
* Memoization. In computing, memoization or memoisation is an
* optimization technique used primarily to speed up computer
* programs by storing the results of expensive function
* calls and returning the cached result when the
* same inputs occur again
*/
// Linear time, test with index as 510 for both the functions
function fibonacciMemoized(index, cache) {
cache = cache || [];
if (cache[index]) {
return cache[index];
} else {
if (index === 1 || index === 0) {
return index;
} else if (index < 0) {
throw new Error('Invalid Position');
} else {
cache[index] =
fibonacciMemoized(index - 1, cache) +
fibonacciMemoized(index - 2, cache);
}
}
return cache[index];
}
/**
* Using the bottom up approach, also known as tabular method
*/
function fibonacciTabular(n) {
const table = [0, 1];
if (n === 1 || n === 0) {
return n;
} else if (n < 0) {
throw new Error('Invalid Position');
}
for (let i = 2; i <= n; i += 1) {
table[i] = table[i - 1] + table[i - 2];
}
return table[n];
}
// const number = 50;
// console.log(`Fib normal - ${fibonacci(number)}`);
// console.log('--');
// console.log(`Fib memo - ${fibonacciMemoized(number)}`);
// console.log(`Fib table - ${fibonacciTabular(number)}`);