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

# ft\_find\_next\_prime

**Objective:**

Create a function that finds and returns the smallest prime number that is greater than or equal to the given input number.

**Turn-in Directory:**

`ex07/`

**Files to Turn In:**

`ft_find_next_prime.c`

**Allowed Functions:**

None

**Prototype:**

```c
int ft_find_next_prime(int nb);
```

**Detailed Explanation:**

Your task is to implement a function that takes an integer `nb` as input and returns the next prime number (i.e., the smallest prime number that is greater than or equal to `nb`). You will likely need to reuse or adapt your `ft_is_prime` logic (or create a helper function) to determine if a number is prime. If the input number is already prime, your function should simply return it. If not, you need to increment the number until you find the next prime.

**Code Implementation:**

```c
int	ft_find_prime(int nb)
{
	int	check; // Variable to check for divisibility

	check = 2; // Start checking from 2
	if (nb <= 1)
		return (0); // Numbers less than or equal to 1 are not prime
	while (check <= nb / 2) // Check divisibility up to nb/2
	{
		if (nb % check == 0)
			return (0); // If divisible, nb is not prime
		check++; // Increment to the next potential divisor
	}
	return (1); // If no divisors found, nb is prime
}

int	ft_find_next_prime(int nb)
{
	if (ft_find_prime(nb)) // Check if nb is already prime
		return (nb);        // If so, return nb
	return (ft_find_next_prime(nb + 1)); // Otherwise, check the next number recursively
}
```

**Example Usage:**

```c
#include <stdio.h>

int ft_find_next_prime(int nb);

int main(void)
{
    printf("Next prime after 12: %d\n", ft_find_next_prime(12));   // Output: 13
    printf("Next prime after 13: %d\n", ft_find_next_prime(13));   // Output: 13
    printf("Next prime after 1: %d\n", ft_find_next_prime(1));     // Output: 2
    printf("Next prime after 0: %d\n", ft_find_next_prime(0));     // Output: 2
    printf("Next prime after -5: %d\n", ft_find_next_prime(-5));   // Output: 2

    return 0;
}
```

**Edge Cases to Consider:**

* **Numbers Less Than or Equal to 1:** The smallest prime number is 2.
  * Input: `1` → Returns `2`
  * Input: `0` → Returns `2`
  * Input: `-5` → Returns `2`
* **Prime Numbers:** If the input is already prime, return the same number.
  * Input: `13` → Returns `13`
  * Input: `7919` → Returns `7919`
* **Non-Prime Numbers:** Find the next prime.
  * Input: `4` → Returns `5`
  * Input: `10` → Returns `11`

**Limitations of `ft_find_next_prime`:**

* **Potential Stack Overflow:** The recursive approach can lead to a stack overflow for very large input numbers that require many recursive calls to find the next prime. An iterative solution would be more robust.

**How It Works:**

1. **`ft_find_prime(nb)` Function:**
   * This helper function determines if a number `nb` is prime.
   * It first handles the base case: if `nb` is less than or equal to 1, it's not prime, so it returns `0`.
   * It then iterates from 2 up to `nb / 2`, checking if `nb` is divisible by any number in this range. If it finds a divisor, `nb` is not prime, and it returns `0`.
   * If the loop completes without finding any divisors, `nb` is prime, and it returns `1`.
2. **`ft_find_next_prime(nb)` Function:**
   * It first calls `ft_find_prime(nb)` to check if `nb` is already prime.
   * If `ft_find_prime(nb)` returns `1` (meaning `nb` is prime), the function immediately returns `nb`.
   * Otherwise, it recursively calls itself with `nb + 1` to check the next number. This process continues until a prime number is found.

**Key Points to Remember:**

* **Prime Number Check:** You need an efficient way to determine if a number is prime.
* **Recursive Approach:** The function calls itself to find the next prime. Be mindful of the potential for stack overflow with very large inputs.
* **Base Case:** The recursion needs a base case (when a prime number is found) to stop.
* **Smallest Prime:** Remember that 2 is the smallest prime number.


---

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