forked from SiZapPaaiGwat/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.js
More file actions
40 lines (36 loc) · 778 Bytes
/
fibonacci.js
File metadata and controls
40 lines (36 loc) · 778 Bytes
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
/**
* Nth number of fibonacci's sequence
*
* Returns the nth number of fibonacci's sequence.
*
* @public
*
* @example
* var fibonacci = require('path-to-algorithms/src/others/fibonacci').fibonacci;
* var nth = fibonacci(20);
*
* console.log(nth); // 6765
*
* @param {Number} n The nth position in fibonacci's sequence
*
* @module others/fibonacci
*/
(function (exports) {
'use strict';
function fibonacci (n) {
if (n > 97) {
throw 'Input too large, results in inaccurate fibonacci value.';
}
var n1 = 0;
var n2 = 1;
var aux;
while (n > 0) {
aux = n1;
n1 = n2;
n2 += aux;
n = n - 1;
}
return n1;
}
exports.fibonacci = fibonacci;
})(typeof window === 'undefined' ? module.exports : window);