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

# ft\_is\_prime

**Objective:**

Create a function that determines if a given number is a prime number. The function should return `1` if the number is prime, and `0` if it is not.

**Turn-in Directory:**

`ex06/`

**Files to Turn In:**

`ft_is_prime.c`

**Allowed Functions:**

None

**Prototype:**

```c
int ft_is_prime(int nb);
```

**Detailed Explanation:**

A prime number is a whole number greater than 1 that has only two divisors: 1 and itself. Your `ft_is_prime` function needs to check if the input number `nb` meets this condition. Consider these points:

* Numbers less than or equal to 1 are *not* prime.
* To determine if a number `nb` is prime, you only need to check for divisibility from 2 up to `nb / 2`. If `nb` is divisible by any number in this range, it is not prime.
* If the loop completes without finding any divisors, the number is prime.

**Code Implementation:**

```c
int	ft_is_prime(int nb)
{
	int	is_p;

	is_p = 2;
	if (nb <= 1)
		return (0);
	while (is_p <= nb / 2)
	{
		if (nb % is_p == 0)
			return (0);
		is_p++;
	}
	return (1);
}
```

**Example Usage:**

```c
#include <stdio.h>

int ft_is_prime(int nb);

int main(void)
{
    printf("Is 7 prime? %d\n", ft_is_prime(7));   // Output: 1
    printf("Is 10 prime? %d\n", ft_is_prime(10));  // Output: 0
    printf("Is 1 prime? %d\n", ft_is_prime(1));   // Output: 0
    printf("Is 2 prime? %d\n", ft_is_prime(2));   // Output: 1
    printf("Is -5 prime? %d\n", ft_is_prime(-5));  // Output: 0

    return 0;
}
```

**Edge Cases to Consider:**

* **Numbers Less Than or Equal to 1:** These are not prime.
  * Input: `1` → Returns `0`
  * Input: `0` → Returns `0`
  * Input: `-5` → Returns `0`
* **The Number 2:** 2 is the smallest prime number.
  * Input: `2` → Returns `1`
* **Prime Numbers:**
  * Input: `7` → Returns `1`
  * Input: `7919` → Returns `1`
* **Non-Prime Numbers:**
  * Input: `4` → Returns `0`
  * Input: `10` → Returns `0`
  * Input: `100` → Returns `0`

**Limitations of `ft_is_prime`:**

* **Integer Overflow:** For very large numbers, the `nb / 2` calculation could potentially lead to issues if `nb` is close to the maximum integer value, though this is unlikely to be a major concern for this exercise.

**How It Works:**

1. **Initialization:** The variable `is_p` is initialized to 2. This variable will be used to check for potential divisors of `nb`.

   ```c
   is_p = 2;
   ```
2. **Base Case:** If `nb` is less than or equal to 1, it's not prime, so the function immediately returns 0.

   ```c
   if (nb <= 1)
       return (0);
   ```
3. **Divisibility Check:** The `while` loop iterates from 2 up to `nb / 2`. In each iteration:
   * It checks if `nb` is divisible by `is_p` without any remainder (`nb % is_p == 0`).
   * If it finds a divisor, `nb` is not prime, and the function returns 0.

     ```c
     if (nb % is_p == 0)
         return (0);
     ```
   * If `nb` is not divisible by `is_p`, the loop continues to the next potential divisor by incrementing `is_p`.

     ```c
     is_p++;
     ```
4. **Prime Determination:** If the loop completes without finding any divisors, it means `nb` is only divisible by 1 and itself, so the function returns 1, indicating that `nb` is prime.

   ```c
   return (1);
   ```

**Key Points to Remember:**

* **Definition of Prime:** Understand the fundamental definition of a prime number.
* **Optimization:** The loop only iterates up to `nb / 2` because a number cannot have a divisor greater than half of itself (excluding itself).
* **Base Cases:** Handling numbers less than or equal to 1 is crucial.
* **Return Values:** Ensure your function returns `1` for prime numbers and `0` for non-prime numbers.
* **Iterative Approach:** The function utilizes a `while` loop for checking potential divisors.


---

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