-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
data-structures-and-algorithms/nth-fibonacci-number/answer/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Nth fibonacci number | ||
|
||
The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... | ||
|
||
Given a number n, print n-th Fibonacci Number. | ||
|
||
Examples: | ||
|
||
``` | ||
Input: 0 | ||
Output: 0 | ||
Input: 1 | ||
Output: 1 | ||
Input: 2 | ||
Output: 1 | ||
Input: 3 | ||
Output: 2 | ||
Input: 9 | ||
Output: 34 | ||
``` |
30 changes: 30 additions & 0 deletions
30
data-structures-and-algorithms/nth-fibonacci-number/answer/nth-fibonacci-number.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const assert = require('assert'); | ||
|
||
function nthFibonacciNumber(n) { | ||
if (n == 0) { | ||
return 0; | ||
} | ||
|
||
if (n == 1) { | ||
return 1; | ||
} | ||
|
||
let secondLast = 0; | ||
let last = 1; | ||
let current = null; | ||
for (let i = 1; i < n; i++) { | ||
current = secondLast + last; | ||
secondLast = last; | ||
last = current; | ||
} | ||
|
||
return last; | ||
} | ||
|
||
assert.equal(nthFibonacciNumber(0), 0); | ||
assert.equal(nthFibonacciNumber(1), 1); | ||
assert.equal(nthFibonacciNumber(2), 1); | ||
assert.equal(nthFibonacciNumber(3), 2); | ||
assert.equal(nthFibonacciNumber(9), 34); | ||
|
||
console.log("All assertions passed!"); |
24 changes: 24 additions & 0 deletions
24
data-structures-and-algorithms/nth-fibonacci-number/exercise/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Nth fibonacci number | ||
|
||
The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... | ||
|
||
Given a number n, print n-th Fibonacci Number. | ||
|
||
Examples: | ||
|
||
``` | ||
Input: 0 | ||
Output: 0 | ||
Input: 1 | ||
Output: 1 | ||
Input: 2 | ||
Output: 1 | ||
Input: 3 | ||
Output: 2 | ||
Input: 9 | ||
Output: 34 | ||
``` |
12 changes: 12 additions & 0 deletions
12
data-structures-and-algorithms/nth-fibonacci-number/exercise/nth-fibonacci-number.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const assert = require('assert'); | ||
|
||
function nthFibonacciNumber(n) { | ||
} | ||
|
||
assert.equal(nthFibonacciNumber(0), 0); | ||
assert.equal(nthFibonacciNumber(1), 1); | ||
assert.equal(nthFibonacciNumber(2), 1); | ||
assert.equal(nthFibonacciNumber(3), 2); | ||
assert.equal(nthFibonacciNumber(9), 34); | ||
|
||
console.log("All assertions passed!"); |