> 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_sqrt.md).

# ft\_sqrt

**Objective:**

Create a function that calculates the **integer square root** of a given number `nb`. If `nb` is not a perfect square, the function should return **0**.

**Turn-in Directory:**

ex05/

**Files to Turn In:**

ft\_sqrt.c

**Allowed Functions:**

None

**Prototype:**

```c
int ft_sqrt(int nb);
```

***

#### **Detailed Explanation**

The **ft\_sqrt** function calculates the **integer square root** of a given number `nb`. The square root of a number `nb` is a value `i` such that `i * i = nb`. If `nb` is not a perfect square, the function returns **0**.

The function returns:

* The **integer square root** of `nb` if `nb` is a perfect square.
* **0** if `nb` is not a perfect square or if `nb` is negative.

***

#### **Code Implementation**

```c
int ft_sqrt(int nb)
{
    int sqrt;
    int i;

    i = 0;
    sqrt = 0;
    while (sqrt <= nb)
    {
        sqrt = i * i;
        if (sqrt == nb)  // Perfect square found
            return (i);
        else if (i > 46340)  // Prevent overflow (46340 is the max integer whose square fits in an int)
            return (0);
        i++;
    }
    return (0);  // No perfect square found
}
```

***

#### **Example Usage**

```c
#include <stdio.h>

int ft_sqrt(int nb);

int main(void)
{
    printf("Square root of 16: %d\n", ft_sqrt(16));  // Output: 4
    printf("Square root of 25: %d\n", ft_sqrt(25));  // Output: 5
    printf("Square root of 10: %d\n", ft_sqrt(10));  // Output: 0 (not a perfect square)
    printf("Square root of -9: %d\n", ft_sqrt(-9));  // Output: 0 (negative input)
    printf("Square root of 2147395600: %d\n", ft_sqrt(2147395600)); // Output: 46340 (max valid input)

    return 0;
}
```

***

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

1. **Perfect Squares**:
   * Input: `16` → Returns **4** (`4 * 4 = 16`).
   * Input: `25` → Returns **5** (`5 * 5 = 25`).
2. **Non-Perfect Squares**:
   * Input: `10` → Returns **0** (no integer square root exists).
3. **Negative Numbers**:
   * Input: `-9` → Returns **0** (negative numbers do not have real square roots).
4. **Large Numbers**:
   * Input: `2147395600` → Returns **46340** (`46340 * 46340 = 2147395600`).

***

#### **Limitations of ft\_sqrt**

1. **No Handling of Negative Inputs**:
   * The function does not handle negative inputs and returns **0** for such cases.
2. **No Handling of Non-Integer Square Roots**:
   * The function only returns integer square roots. If `nb` is not a perfect square, it returns **0**.
3. **Overflow Prevention**:
   * The function includes a check to prevent overflow by limiting `i` to **46340**, which is the maximum integer whose square fits in an `int`.

***

#### **How It Works**

1. **Initialization**:

   * The variables `sqrt` and `i` are initialized to **0**.

   ```c
   i = 0;
   sqrt = 0;
   ```
2. **Loop Through Possible Square Roots**:

   * The `while` loop iterates through possible values of `i` and calculates `sqrt = i * i`.

   ```c
   while (sqrt <= nb)
   {
       sqrt = i * i;
   ```
3. **Perfect Square Check**:

   * If `sqrt == nb`, the function returns `i` (perfect square found).

   ```c
   if (sqrt == nb)
       return (i);
   ```
4. **Overflow Prevention**:

   * If `i` exceeds **46340**, the function returns **0** to prevent overflow.

   ```c
   else if (i > 46340)
       return (0);
   ```
5. **Increment and Continue**:

   * If no perfect square is found, `i` is incremented, and the loop continues.

   ```c
   i++;
   ```
6. **No Perfect Square Found**:

   * If the loop ends without finding a perfect square, the function returns **0**

   ```c
   return (0);
   ```

***

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

1. **Iterative Approach**:
   * The function uses a `while` loop to iterate through possible square roots.
2. **Overflow Prevention**:
   * The function includes a check to prevent overflow by limiting `i` to **46340**.
3. **Efficiency**:
   * The time complexity is **O(sqrt(n))**, where `n` is the input number. The loop runs until `i * i` exceeds `nb`.

***


---

# 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_sqrt.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.
