-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_nbprime.c
87 lines (80 loc) · 1.81 KB
/
ft_nbprime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nbprime.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybouzgao <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/12 15:30:15 by ybouzgao #+# #+# */
/* Updated: 2017/11/13 19:58:37 by ybouzgao ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int *ft_nbprimebis(int *tab, int i, int j, int nb)
{
int l;
int k;
i = 1;
l = 0;
if (nb <= 1)
return (tab);
while (i++ <= nb)
{
j = 1;
k = 0;
while (j * j < i)
{
if ((i % j) == 0)
k++;
j++;
}
if (k == 1)
{
tab[l] = i;
l++;
}
}
tab[l] = '\0';
return (tab);
}
static void ft_show(int *tab, int l, int nb)
{
int i;
i = 0;
while (i < l)
{
ft_putnbr(tab[i]);
ft_putchar('\n');
i++;
}
ft_putstr("il y a ");
ft_putnbr(l);
ft_putstr(" nombres premier inferieur ou egal a ");
ft_putnbr(nb);
}
int *ft_nbprime(int nb)
{
int i;
int j;
int k;
int *tab;
int l;
i = 0;
l = 0;
if (nb == 0 || nb == 1)
return (0);
while (++i <= nb)
{
j = 0;
k = 0;
while (++j < i)
k += ((i % j) == 0) ? 1 : 0;
if (k == 1)
l++;
}
if ((tab = (int*)malloc(sizeof(int) * (l + 1))) == 0)
return (NULL);
tab = ft_nbprimebis(tab, i, j, nb);
ft_show(tab, l, nb);
return (tab);
}