-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.js
More file actions
51 lines (47 loc) · 1.52 KB
/
Copy pathmath.js
File metadata and controls
51 lines (47 loc) · 1.52 KB
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
// simplistic Fibonacci implementation with recursion
const fibonacci = (n) => {
if (n === 1) return 1;
else if (n === 2) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
};
// more efficient Fibonacci implementation with a loop
const fibonacciLoop = (n) => {
const fibos = [];
fibos[0] = 1;
fibos[1] = 1;
fibos[2] = 1;
for (let i = 3; i <= n; i += 1) {
fibos[i] = fibos[i - 1] + fibos[i - 2];
}
return fibos[n];
};
// This converts the Fibonacci function from a synchronous function to an asynchronous
// function one with a callback.
// Using setImmediate, each stage of the calculation is managed through Node.js's
// event loop and the server can easily handle other requests while churning away
// on a calculation. It does nothing to reduce the computation required; this is still the silly,
// inefficient Fibonacci algorithm. All we've done is spread the computation through the event loop.
const fibonacciAsync = (n, done) => {
if (n === 0) {
done(undefined, 0);
} else if (n === 1 || n === 2) {
done(undefined, 1);
} else {
setImmediate(() => {
fibonacciAsync(n - 1, (err, val1) => {
if (err) done(err);
else {
setImmediate(() => {
fibonacciAsync(n - 2, (err, val2) => {
if (err) done(err);
else done(undefined, val1 + val2);
});
});
}
});
});
}
};
module.exports.fibonacci = fibonacci;
module.exports.fibonacciLoop = fibonacciLoop;
module.exports.fibonacciAsync = fibonacciAsync;