Skip to content

Commit

Permalink
Merge pull request #426 from sir-gon/feature/ctci_fibonacci_numbers
Browse files Browse the repository at this point in the history
[Hacker Rank] Interview Preparation Kit: Recursion: Fibonacci Numbers…
  • Loading branch information
sir-gon authored Aug 6, 2024
2 parents 308d566 + ffcfbd6 commit 245ca59
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# [Recursion: Fibonacci Numbers](https://www.hackerrank.com/challenges/ctci-fibonacci-numbers)

- Difficulty: `#easy`
- Category: `#ProblemSolvingBasic`

## The Fibonacci Sequence

The Fibonacci sequence appears in nature all around us,
in the arrangement of seeds in a sunflower and the spiral of a nautilus for example.

The Fibonacci sequence begins with `fibonacci(0) = 0` and `fibonacci(1) = 1`
as its first and second terms. After these first two elements,
each subsequent element is equal to the sum of the previous two elements.

Programmatically:

- `fibonacci(0) = 0`
- `fibonacci(1) = 1`
- `fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)`

Given , return the number in the sequence.

## Example

The Fibonacci sequence to `6` is `fs = [0, 1, 1, 2, 3, 5, 8]`.
With zero-based indexing, `fs[5] = 5`.

## Function Description

Complete the recursive function `fibonacci` in the editor below.

fibonacci has the following parameter(s):

- `int n`: the index of the sequence to return

## Returns

- `int`: the element in the Fibonacci sequence

## Input Format

The integer `n`.

## Constraints

- $ 0 \leq n \leq 30 $

## Sample Input

```text
STDIN Function
----- --------
3 n = 3
```

## Sample Output

```text
2
```

## Explanation

The Fibonacci sequence begins as follows:

```C
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(2) = (0 + 1) = 1
fibonacci(3) = (1 + 1) = 2
fibonacci(4) = (1 + 2) = 3
fibonacci(5) = (2 + 3) = 5
fibonacci(6) = (3 + 5) = 8
```
...
In the sequence above, `fibonacci(3)` is `2`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import { fibonacci } from './ctci_fibonacci_numbers';
import TEST_CASES from './ctci_fibonacci_numbers.testcases.json';

describe('ctci_fibonacci_numbers', () => {
it('fibonacci test cases', () => {
expect.assertions(3);

TEST_CASES.forEach((test) => {
const answer = fibonacci(test.input);

console.debug(`fibonacci(${test.input}) solution found: ${answer}`);

expect(answer).toStrictEqual(test.expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"title": "Sample Test case 0",
"input": 5,
"expected": 5
},
{
"title": "Sample Test case 1",
"input": 12,
"expected": 144
},
{
"title": "Sample Test case 2",
"input": 17,
"expected": 1597
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_fibonacci_numbers.md]]
*/

export function fibonacci(n: number): number {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}

return fibonacci(n - 1) + fibonacci(n - 2);
}

export default { fibonacci };

0 comments on commit 245ca59

Please sign in to comment.