Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 2a12376

Browse files
committed
Merge pull request #27 from vernak2539/master
Implementing better fibonacci sequence generator
2 parents d0fa968 + b98098b commit 2a12376

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

fibonnaci/fibonacci2.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Better Fibonacci, less computing required. Better O(n)
2+
// @index = number spot in array starting at 1
3+
// @n = what number in the fibonacci sequence you want to go to
4+
// by position, not index of array
5+
// i.e. positions start at 1
6+
var fibonnaci = function( n ) {
7+
var results = [0, 1];
8+
9+
if( n > 2 ) {
10+
for( var i = 2; i < n; i++ ){
11+
results[ i ] = results[ i - 2 ] + results[ i - 1 ];
12+
}
13+
}
14+
return results[n - 1];
15+
};

0 commit comments

Comments
 (0)