Skip to content

Commit 9d3fd48

Browse files
authored
Merge pull request #1 from EliasAfara/feature/new-recursion-algorithms
Feature/new recursion algorithms
2 parents 6e6a0ac + 8739825 commit 9d3fd48

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

.github/workflows/UpdateDirectory.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: 🤓 Commit & push new Directory (if needed)
3131
run: |
3232
if [[ `git status --porcelain` ]]; then
33-
git commit -am "Updated Documentation in README.md"
33+
git commit -am "Updated Documentation in DIRECTORY.md"
3434
git push
3535
else
3636
echo "NO CHANGES DETECTED"

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- **Greedy-Algorithms**
1111
- [MinimumWaitingTime](Greedy-Algorithms/MinimumWaitingTime.js)
1212

13+
- **Recursion**
14+
- [NthFibonacci](Recursion/NthFibonacci.js)
15+
1316
- **Searching**
1417
- [BinarySearch](Searching/BinarySearch.js)
1518
- [ThreeLargestNumbers](Searching/ThreeLargestNumbers.js)

Recursion/NthFibonacci.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Get the nth Fibonacci number.
3+
*
4+
* @param {number} n - The position of the Fibonacci number to find.
5+
* @returns {number} - The nth Fibonacci number.
6+
*
7+
* @description
8+
* The Fibonacci sequence is defined as follows: the first number of the sequence is 0, the second number is 1,
9+
* and the nth number is the sum of the (n - 1)th and (n - 2)th numbers.
10+
*
11+
* For the purpose of this function, the first Fibonacci number is F0; therefore, getNthFib(1) is equal to F0,
12+
* getNthFib(2) is equal to F1, and so on.
13+
*
14+
* The function uses a recursive approach to calculate the Fibonacci number for a given position.
15+
*
16+
* @Complexity
17+
* The time complexity of this function is O(2^n), which makes it inefficient for large values of n.
18+
* This is because it recalculates Fibonacci numbers multiple times for the same values.
19+
*/
20+
function getNthFib(n) {
21+
if (n === 0) return 0;
22+
if (n === 1) return 0;
23+
if (n === 2) return 1;
24+
return getNthFib(n - 1) + getNthFib(n - 2);
25+
}
26+
27+
export { getNthFib };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getNthFib } from "../NthFibonacci";
2+
3+
describe("getNthFib", () => {
4+
it("should return the correct Fibonacci number for a given position", () => {
5+
expect(getNthFib(0)).toBe(0);
6+
expect(getNthFib(1)).toBe(0);
7+
expect(getNthFib(2)).toBe(1);
8+
expect(getNthFib(3)).toBe(1);
9+
expect(getNthFib(4)).toBe(2);
10+
expect(getNthFib(5)).toBe(3);
11+
expect(getNthFib(6)).toBe(5);
12+
expect(getNthFib(7)).toBe(8);
13+
expect(getNthFib(8)).toBe(13);
14+
expect(getNthFib(9)).toBe(21);
15+
expect(getNthFib(10)).toBe(34);
16+
expect(getNthFib(15)).toBe(377);
17+
expect(getNthFib(20)).toBe(4181);
18+
expect(getNthFib(25)).toBe(46368);
19+
expect(getNthFib(30)).toBe(514229);
20+
});
21+
});

0 commit comments

Comments
 (0)