/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n = 0;
printf("Crible dynamique\n");
printf("Entrer la taille du tableau : ");
if(scanf("%d", &n)!= 1)
{
printf("Erreur lors de la saisie\n");
return -1;
}
//VLA
//bool tab[n] ;
//Allocation dynamique
bool *tab = malloc(sizeof(bool)*n);
if (tab == NULL)
{
printf("Échec de l'allocation\n");
return EXIT_FAILURE;
}
for(int i = 0 ; i <n ; i++)
tab[i]=true;
printf("Taille du tableau %lu octets\n", sizeof(bool)*n);
printf("Taille du tableau %lu ko\n", sizeof(bool)*n/1024);
printf("Taille du tableau %lu Mo\n", sizeof(bool)*n/(1024*1024));
tab[1] = false;
for (int i = 2 ; i < sqrt(n) ; i ++ ) {
if(tab[i] == true) {
for (int mul = i+i ; mul <= n ; mul+=i) {
tab[mul] = false;
}
}
}
printf("\nLes nombres premiers sont : \n");
for(int i = 1 ; i < n ; i++) {
if(tab[i]) {
printf("%d ", i);
}
}
printf("\n");
//Allocation dynamique
free(tab);
return 0;
}