> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c05/ft_fibonacci.md).

# ft\_fibonacci

**Objective:**

Create a **recursive function** that returns the **n-th element** of the Fibonacci sequence. The Fibonacci sequence starts with `0, 1, 1, 2, ...`, where each number is the sum of the two preceding ones. The function should return **-1** if the index is negative.

**Turn-in Directory:**

ex04/

**Files to Turn In:**

ft\_fibonacci.c

**Allowed Functions:**

None

**Prototype:**

```c
int ft_fibonacci(int index);
```

***

#### **Detailed Explanation**

The **ft\_fibonacci** function calculates the **n-th element** of the Fibonacci sequence using recursion. The Fibonacci sequence is defined as:

* `F(0) = 0`
* `F(1) = 1`
* `F(n) = F(n-1) + F(n-2)` for `n > 1`

The function returns:

* The **n-th Fibonacci number** if `index` is a non-negative integer.
* **-1** if `index` is negative.

***

#### **Code Implementation**

```c
int ft_fibonacci(int index)
{
    if (index < 0)  // Invalid index (negative number)
        return (-1);
    else if (index == 0)  // Base case: F(0) = 0
        return (0);
    else if (index == 1 || index == 2)  // Base case: F(1) = 1, F(2) = 1
        return (1);
    else  // Recursive case: F(n) = F(n-1) + F(n-2)
        return (ft_fibonacci(index - 1) + ft_fibonacci(index - 2));
}
```

***

#### **Example Usage**

```c
#include <stdio.h>

int ft_fibonacci(int index);

int main(void)
{
    printf("Fibonacci(0): %d\n", ft_fibonacci(0));  // Output: 0
    printf("Fibonacci(1): %d\n", ft_fibonacci(1));  // Output: 1
    printf("Fibonacci(2): %d\n", ft_fibonacci(2));  // Output: 1
    printf("Fibonacci(5): %d\n", ft_fibonacci(5));  // Output: 5
    printf("Fibonacci(10): %d\n", ft_fibonacci(10)); // Output: 55
    printf("Fibonacci(-3): %d\n", ft_fibonacci(-3)); // Output: -1

    return 0;
}
```

***

#### **Edge Cases to Consider**

1. **Negative Index**:
   * Input: `-3` → Returns **-1** (invalid index).
2. **Index 0**:
   * Input: `0` → Returns **0** (`F(0) = 0`).
3. **Index 1 or 2**:
   * Input: `1` or `2` → Returns **1** (`F(1) = 1`, `F(2) = 1`).
4. **Positive Index**:
   * Input: `5` → Returns **5** (`F(5) = 5`).
   * Input: `10` → Returns **55** (`F(10) = 55`).

***

#### **Limitations of ft\_fibonacci**

1. **No Handling of Large Indices**:
   * For large values of `index`, the function may cause a **stack overflow** due to excessive recursion depth.
2. **Inefficiency**:
   * The recursive approach is inefficient for large indices because it recalculates the same Fibonacci numbers multiple times.
3. **No Handling of Overflows**:
   * The function does not handle integer overflows. For large values of `index`, the result may exceed the range of an `int`, leading to undefined behavior.

***

#### **How It Works**

1. **Base Case 1 (Negative Index)**:

   * If `index` is negative, the function immediately returns **-1** (invalid index).

   ```c
   if (index < 0)
       return (-1);
   ```
2. **Base Case 2 (Index 0)**:

   * If `index` is **0**, the function returns **0** (`F(0) = 0`).

   ```c
   else if (index == 0)
       return (0);
   ```
3. **Base Case 3 (Index 1 or 2)**:

   * If `index` is **1** or **2**, the function returns **1** (`F(1) = 1`, `F(2) = 1`).

   ```c
   else if (index == 1 || index == 2)
       return (1);
   ```
4. **Recursive Case**:

   * For any positive integer greater than 2, the function calls itself with `index - 1` and `index - 2` and returns the sum of the two results.

   ```c
   else
       return (ft_fibonacci(index - 1) + ft_fibonacci(index - 2));
   ```

***

#### **Key Points to Remember**

1. **Recursive Approach**:
   * The function uses recursion to calculate the Fibonacci number, calling itself with smaller indices until it reaches the base cases.
2. **Base Cases**:
   * The base cases handle edge cases like `index = 0`, `index = 1`, and `index = 2`.
3. **Efficiency**:
   * The time complexity is **O(2^n)**, where `n` is the index. This is because the function recalculates the same Fibonacci numbers multiple times.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c05/ft_fibonacci.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
