Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f6f5bf8

Browse files
Adding iterative solution for fibonacci
1 parent 7ac271e commit f6f5bf8

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎fibonacci/directions.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# --- Directions
2+
3+
Print out the n-th entry in the fibonacci series.
4+
The fibonacci series is an ordering of numbers where
5+
each number is the sum of the preceeding two.
6+
7+
8+
# ---For example, the sequence
9+
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
10+
forms the first ten entries of the fibonacci series.
11+
12+
13+
# ---Example:
14+
15+
fibonacci(4) should return 3
16+
17+
18+
# --- Solution
19+
20+
//Solution No 1 using iterative solution
21+

‎fibonacci/fibonacci.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// --- Directions
2+
// Print out the n-th entry in the fibonacci series.
3+
// The fibonacci series is an ordering of numbers where
4+
// each number is the sum of the preceeding two.
5+
// For example, the sequence
6+
// [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
7+
// forms the first ten entries of the fibonacci series.
8+
// Example:
9+
// fibonacci(4) should return 3
10+
11+
12+
//Solution No 1 using iterative solution
13+
function fibonacci(n) {
14+
const result = [0, 1];
15+
16+
for(let i=2; i <= n; i++) {
17+
const a = result[i-1];
18+
const b = result[i-2];
19+
20+
result.push(a+b);
21+
}
22+
23+
return result[n];
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /