piscine/c05/ex07/ft_find_next_prime.c
Willem Haffmans 607ce08c18 all
2024-09-10 00:18:01 +02:00

63 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_find_next_prime.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 12:40:48 by whaffman #+# #+# */
/* Updated: 2024/06/13 13:09:20 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_is_prime(int nb)
{
int i;
if (nb < 2)
return (0);
i = 2;
while (i * i <= nb)
{
if (nb % i == 0)
return (0);
i++;
}
return (1);
}
int ft_find_next_prime(int nb)
{
nb++;
while (1)
{
if (ft_is_prime(nb))
return (nb);
nb++;
}
return (0);
}
#ifdef DEBUG
void test(int nb)
{
printf("After %d the next prime is %d.\n", nb, ft_find_next_prime(nb));
}
int main(void)
{
test(3);
test(31);
test(4);
test(29);
test(103);
test(256);
test(123456783);
return (0);
}
#endif