forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFibonacci.js
97 lines (73 loc) · 2.42 KB
/
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence,
such that each number is the sum of the two preceding ones, starting from 0 and 1
Fibonacci Series - 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ....
Below is the program for implementing fibonacci series in 3 different ways
*/
// Prompt node package for taking user input
const prompt = require("prompt-sync")({ sigint: true });
// Number of steps required to complete fora given fibonacci function
let iterativeCount = 0; // iterative function count
let recursiveCount = 0; // recursive function count
let memoizedCount = 0; // memoized function count
// Iterative function ( Looping )
function fibonacciIterative(n) {
let array = [0, 1];
for (let i = 2; i < n + 1; i++) {
iterativeCount++;
array.push(array[i - 2] + array[i - 1]);
}
return array[n];
}
// Recursive function
function fibonacciRecursive(n) {
recursiveCount++;
if (n < 2) {
return n;
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
// Memoization ( a technique which attempts to increase function's performance by caching its previously computed results)
function fibonacciMemoized() {
let cache = {}; // store previously computed results
return function fib(n) {
memoizedCount++;
if (n in cache) {
return cache[n];
} else {
if (n < 2) {
return n;
} else {
cache[n] = fib(n - 1) + fib(n - 2);
return cache[n];
}
}
};
}
/* Workflow of user input */
// Take the fibonacci number as input
let fibKey = +prompt("Enter number to find fibonacci series value - ");
// Check whether the entered value is number or not
if (isNaN(fibKey)) return console.log("Only numbers are allowed");
// Iterative Result
let result = fibonacciIterative(fibKey);
console.log(
`Fibonacci Iterative result in ${iterativeCount} steps - ${result}`
);
// Memoization Result
let fasterFib = fibonacciMemoized(fibKey);
result = fasterFib(fibKey);
console.log(`Fibonacci Memoized result in ${memoizedCount} steps - ${result}`);
// Recursive Result
result = fibonacciRecursive(fibKey);
console.log(
`Fibonacci Recursive result in ${recursiveCount} steps - ${result}`
);
// Sample I/O
/*
> node Fibonacci
Enter number to find fibonacci series value - 25
Fibonacci Iterative result in 24 steps - 75025
Fibonacci Memoized result in 49 steps - 75025
Fibonacci Recursive result in 242785 steps - 75025
*/