aboutsummaryrefslogtreecommitdiff
path: root/c05/ex06/ft_is_prime.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-08-06 16:56:43 +0200
committerCharles <sircharlesaze@gmail.com>2019-08-06 16:56:43 +0200
commit056a88ee9c2c9e26f9712d14c645e367d851f394 (patch)
tree52b54df3e809e1526325dd9123078a9cc01e9831 /c05/ex06/ft_is_prime.c
parente534578eecebd27c4e7e235ce85961446e6c2e43 (diff)
downloadpiscine-056a88ee9c2c9e26f9712d14c645e367d851f394.tar.gz
piscine-056a88ee9c2c9e26f9712d14c645e367d851f394.tar.bz2
piscine-056a88ee9c2c9e26f9712d14c645e367d851f394.zip
c05
- primality test correction for ft_is_prime and ft_find_next_prime - queens puzzle solved with backtracking (similar to rush01)
Diffstat (limited to 'c05/ex06/ft_is_prime.c')
-rw-r--r--c05/ex06/ft_is_prime.c14
1 files changed, 5 insertions, 9 deletions
diff --git a/c05/ex06/ft_is_prime.c b/c05/ex06/ft_is_prime.c
index 3c83061..658fefb 100644
--- a/c05/ex06/ft_is_prime.c
+++ b/c05/ex06/ft_is_prime.c
@@ -13,7 +13,6 @@
int ft_is_prime(int nb)
{
long unsigned int i;
- long unsigned int nbu;
if (nb <= 1)
return (0);
@@ -21,15 +20,12 @@ int ft_is_prime(int nb)
return (1);
if (nb % 2 == 0 || nb % 3 == 0)
return (0);
- i = 1;
- nbu = nb;
- while (i * i <= nbu) // ne fonctionne pas de 7 a 41
+ i = 5;
+ while (i * i <= nb)
{
- if (nbu % (i * 6 - 1) == 0)
- return (0);
- if (nbu % (i * 6 + 1) == 0)
- return (0);
- i += 1;
+ if (nb % i == 0 || nb % (i + 2) == 0)
+ return (0);
+ i += 6;
}
return (1);
}