Skip to content

Commit c2eec66

Browse files
committed
--update: added fibonacci
1 parent 20280c8 commit c2eec66

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/_Classics_/fibonacci/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// the algorithm has time complexity of O(n^2), very bad!
2+
function fibonacci(position) {
3+
// if position is 1 or 2, the number in fibonacci sequence will be 1
4+
if (position < 3) {
5+
return 1;
6+
}
7+
// else the element in fibonacci sequence will be the sum of
8+
// element at position(p) (p -1) and (p - 2)
9+
return fibonacci(position - 2) + fibonacci(position - 1);
10+
}

0 commit comments

Comments
 (0)