#include <stdio.h>
int somme(int tableau[], int taille) {
int i;
int accumulateur;
accumulateur = 0;
for (i = 0; i < taille; i++) {
accumulateur = accumulateur + tableau[i];
}
return accumulateur;
}
void affiche(int tableau[], int taille) {
for (int i = 0; i < taille; ++i) {
printf("La case %d contient la valeur : %d.\n", i, tableau[i]);
}
}
int main() {
int notes[5] = { 12,18,15,13,20 };
int total = somme(notes, 5);
printf("Le total est : %d. Au revoir.\n", total);
affiche(notes, 5);
return 0;
}
#include <stdio.h>
/* I */
int somme(int tableau[], int taille) {
int i;
int accumulateur;
accumulateur=0;
for (i=0;i<taille;i++) {
accumulateur=accumulateur+ tableau[i];
}
return accumulateur;
}
void affiche(int tableau[], int taille){
printf("[|");
int i;
for (i=0; i<taille-1;i++){
printf(" %d ,",tableau[i]);
}
printf(" %d |]",tableau[taille-1]);
}
/* II - 1) */
void crible(int tab[],int taille,int p){
int i;
for (i=2; p*i<taille;i++){
tab[p*i]=0;
}
}
void affichage(int tab[],int taille){
int i;
int compt = 0;
for (i=0;i<taille;i++){
if (tab[i]!=0) {
printf("%d ",tab[i]);
compt++;
}
}
printf("\n %d",compt) ;
}
void erathosthene(int n){
int tab[n];
int i;
for (i=0;i<n;i++){
tab[i]=i;
}
tab[1]=0;
for (i=0;i<n;i++){
if (tab[i]!=0) crible(tab,n,i);
}
affichage(tab,n);
}
/* II - 2) */
void calc_sum(char expression[]){ /* ATTENTION : En ASCII, les chiffres sont de 48 à 57 */
int out=0;
int temp=0;
int valide=1;
int dec=0;
int c;
int i;
for (c=0 ; expression[c]!= '\0' ;c++) {
printf("%c",expression[c]);
if (expression[c]=='+') {
if (temp==0)
valide=0;
else {
for (i=0;i<dec-1;i++)
temp=temp/10;
out+=temp;
temp=0;
}
}
else {
if ('0'<=expression[c] && expression[c]<='9') {
temp=temp*10 + (expression[c]-'0');
if (dec!=0)
dec++;
}
else
if (expression[c]=='.')
dec=1;
else
valide=0;
}
}
if (temp!=0)
out+=temp;
else
valide=0;
if (valide==1)
printf("\n%d",out);
else
printf("\nBye");
}
int main(){
/* int notes[5] = {12,18,15,13,20};
int total = somme(notes,5);
printf("Le total est : %d. Au revoir \n",total);
return 0;
affiche(notes,5);
erathosthene(100);*/
calc_sum("45+3+22+1.2");
}
#include <stdio.h>
#include <stdlib.h>
//#include <bool.h>
#include <math.h>
void mult(int k, int* tab, int n, int nb) {
if (nb>=n) return;
tab[nb-1] = k;
mult(k, tab, n, nb+k);
}
int main() {
int n;
scanf("%d", &n);
//printf("%d\n", n);
int* tab = malloc((n-1)*sizeof(int));
for (int i=0; i<n-1; ++i) tab[i] = 0;
for (int i=2; i<n; i++) {
mult(i, tab, n, i+i);
}
//for (int i=0; i<n-1; ++i) printf("%d ", tab[i]);
//printf("\n");
int pi = 0;
for (int i=1; i<n-1; ++i) {
if(tab[i] == 0) {printf("%d ", i+1); pi++;}
}
printf("\n");
printf("%d\n", pi);
free(tab);
return EXIT_SUCCESS;
}
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int parser(char* str){
int n = strlen(str);
float* res = malloc(n);
for (int i = 0; i < n; i++){
res[i] = 0;
}
int malParsee = 0;
int j = 0; //vérifie le parenthésage (+) des expressions
int actuel = 0;
char enCours[200];
enCours[0] = '\0';
for (int i = 0; i < n; i++){
char c = (char)str[i];
if (j == 1){
if (c == '+'){
res[actuel] = atof(enCours);
enCours[0] = '\0';
actuel++;
j = 0;
}else{
if (c == '\n'){
res[actuel] = atof(enCours);
enCours[0] = '\0';
actuel++;
}
char new_c[2] = "a";
new_c[0] = c;
strcat(enCours, new_c);
}
}else{
if (c == '+' || c == '\n'){
malParsee = 1;
}else{
char new_c[2] = "\0";
new_c[0] = c;
strcat(enCours, new_c);
j = 1;
}
}
}
if (malParsee){
printf("Bye. \n");
return 0;
}else{
float sum = 0;
for (int i = 0; i < n/2 +1; i++){
sum = sum + res[i];
}
printf("%f\n", sum);
return 1;
}
}
void main(){
char entree[200];
int continuer = 1;
while(continuer){
printf("entrez un calcul : ");
scanf("%s", entree);
continuer = parser(strcat(entree, "\n"));
}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
bool* erat(int n){
bool* res = malloc(n*sizeof(bool));
for(int i = 0; i < n; i+=1){
res[i] = false;
}
for(int i = 1; i < n; i+=1){
if(!res[i]){
for(int j = 2*i + 1 ; j < n ; j+= i+1 ){
res[j] = true;
}
}
}
return res;
}
void eratosthene(int n){
bool* crible = erat(n);
int compteur = 0;
for(int i = 1; i<n ; i+=1){
if(!crible[i]){
printf("%i\n", i+1);
if(i+1!=n) compteur += 1;
}
}
free(crible);
printf("pi(n) : %i\n", compteur);
}
int main(int argc, char* argv[]){
assert(argc == 2);
int n = atoi(argv[1]);
eratosthene(n);
}
#include <stdio.h>
int somme(int tableau[], int taille){
int i;
int accumulateur;
accumulateur=0;
for (i=0; i<taille; i++){
accumulateur=accumulateur+tableau[i];
}
return accumulateur;
}
void affiche(int tableau[], int taille){
printf("{");
for(int i=0; i<taille;i++){
if (i>0) {
printf(", ");
}
printf("%d", tableau[i]);
}
printf("}\n");
}
int main(){
int notes[5]={12,18,15,13,20};
int total=somme(notes, 5);
printf("Le total est : %d. Au revoir.\n", total);
affiche(notes, 5);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/*
* Initializes an eratosthene sieve
*
* @arg primes the pointer to the location in memory where the sieve will be initialized
* @arg size the size of the allocated memory
* @return nothing
*/
void init(char* primes, int size){
primes[0]=primes[1]=0;
for(int i=2; i<size; i++){
primes[i]=1;
}
}
/*
* Displays a table
*
* @arg table a table of characters
* @arg size the size of the table
* @return nothing
*/
void display(char tableau[], int size){
printf("{");
for(int i=2; i<size;i++){
if (isPrime[i]) {
printf("%d\n", i);
}
}
/*
* Unmark non-prime numbers
*
* @arg table a table of characters representinf wether their index is a prime number or not
* @arg size the size of the table
* @return nothing
*/
void sieve(char isPrime[], int size){
for(int p=2; p<size; p++){
if(isPrime[p]){
int i=2*p;
while(i<size){
isPrime[i]=0;
i+=p;
}
}
}
}
int main(int argc, char* *argv){
int size=atoi(argv[1]);
char isPrime[size];
init(isPrime,size);
sieve(isPrime[size];
display(isPrime, size);
}
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 11
#define M 15
char A[N][M];
void init_just_in_case(void) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = 'X';
}
}
}
typedef enum {
DFS_UNSEEN,
DFS_OPEN,
DFS_CLOSED,
} DFS_MARK;
DFS_MARK dfs_marks[N][M];
bool cyclical;
bool connex;
// there must be a problem somewhere in there, because
// cycles don't seem to be detected properly...
void dfs(int i, int j, int parent_i, int parent_j) {
if (dfs_marks[i][j] == DFS_CLOSED)
return;
if (dfs_marks[i][j] == DFS_OPEN) {
cyclical = true;
return;
}
dfs_marks[i][j] = DFS_OPEN;
// issue found: we are not going up or left
if (i + 2 < N - 1 && (j != parent_j || i + 2 != parent_i) && A[i + 1][j] == ' ')
dfs(i + 2, j, i, j);
if (j + 2 < M - 1 && (i != parent_i || j + 2 != parent_j) && A[i][j + 1] == ' ')
dfs(i, j + 2, i, j);
dfs_marks[i][j] = DFS_CLOSED;
}
void update_graph_knowledge(void) {
cyclical = false;
connex = true;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
dfs_marks[i][j] = DFS_UNSEEN;
}
}
dfs(1, 1);
for (int i = 1; i < N - 1; i += 2) {
for (int j = 1; j < M - 1; j += 2) {
if (dfs_marks[i][j] == DFS_UNSEEN) {
connex = false;
}
}
}
}
void generate(void) {
init_just_in_case();
for (int i = 1; i < N - 1; i += 2) {
for (int j = 1; j < M - 1; j += 2) {
A[i][j] = ' ';
}
}
printf("\n");
update_graph_knowledge();
while (cyclical || !connex) {
// remove a random edge
int i = 1 + 2 * (rand() % ((N) / 2));
int j = 1 + 2 * (rand() % ((M) / 2));
if (i == N || j == M)
continue;
if (rand() % 2 && i + 2 < N - 1) {
A[i + 1][j] = ' ';
update_graph_knowledge();
if (cyclical)
A[i + 1][j] = 'X';
} else if (j + 2 < M - 1) {
A[i][j + 1] = ' ';
update_graph_knowledge();
if (cyclical)
A[i][j + 1] = 'X';
}
update_graph_knowledge();
}
}
void print_labyrinth(void) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%c", A[i][j]);
}
printf("\n");
}
}
int main(void) {
srand(time(NULL));
generate();
print_labyrinth();
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
typedef struct cell_s
{
int val;
struct cell_s* next;
} cell;
cell* cons(int x, cell* l)
{
cell* new = (cell*)malloc(sizeof(cell));
new->val = x;
new->next = l;
return new;
}
void eratosthene(int n)
{
int p = 1;
cell** prems = (cell**)malloc(sizeof(cell*));
printf("2 ");
*prems = cons(2, NULL);
for(int i = 3; i<=n; i++)
{
bool prem = true;
for(cell* temp = *prems; temp != NULL; temp = temp->next) if(i%temp->val==0) prem = false;
if(prem)
{
*prems = cons(i, *prems);
printf("%d ", i);
p++;
}
}
printf("\n%d\n", p);
return;
}
void eratosthene2(int n)
{
//A FAIRE AVEC DES BOOL DE TAILLE 4
}
bool is_digit(char c)
{
return(c>47 && c<58);
}
int read_int(char* s, int* i)
{
int res = 0;
while(is_digit(s[*i]))
{
res *= 10;
res += ((int)s[*i] - 48);
(*i)++;
}
return res;
}
int expo(int a, int n)
{
if(n==1) return a;
int x = expo(a, n/2);
if(n%2 == 0) return(x*x);
else return(a*x*x);
}
double read_float(char*s, int* i)
{
double res = (double)read_int(s, i);
if(s[*i] == ',' || s[*i] == '.')
{
(*i)++;
int i0 = *i;
double frac = (double)read_int(s, i)/ (double)expo(10, *i - i0);
return(res + frac);
}
return res;
}
void calc_sum(void)
{
while(1)
{
char in[256];
scanf("%s", in);
bool plus_ok = false; //Est-ce qu'avoir un plus à cet endroit pose un pb
int s = 0;
int i;
for(i = 0; i<256 && in[i] != '\0'; i++)
{
if(in[i]=='+')
{
if(plus_ok)
{
plus_ok = false;
continue;
}
printf("Bye\n");
return;
}
s += read_int(in, &i);
plus_ok = true;
if(in[i] != '+')
{
if(in[i] != '\0')
{
printf("Bye\n");
return;
}
i--;
continue;
}
}
printf("%d\n", s);
}
}
void unite(int* p, int i, int j, int n)
{
if(p[i] != p[j])
{
for(int k = 0; k<n; k++)
{
if(p[k] == p[j]) p[k] = p[i];
}
}
return;
}
bool** genere_laby(int n, int m)
{
assert(n%2 * m%2 == 1);
//true : 1 mur
int* part = (int*)malloc(sizeof(int)*(n*m/4));
for(int i = 0; i<n*m/4; i++) part[i] = i;
bool** lab = (bool**)malloc(sizeof(bool*)*n);
for(int i = 0; i<n; i++)
lab[i] = (bool*)malloc(sizeof(bool)*m);
for(int i = 0; i<n/2; i++)
{
lab[2*i][0] = true;
lab[2*i+1][0] = true;
lab[2*i][m-1] = true;
lab[2*i+1][m-1] = true;
for(int j = 0; j<m/2; j++)
{
if(i==0)
{
lab[0][2*j] = true;
lab[0][2*j+1] = true;
lab[n-1][2*j] = true;
lab[n-1][2*j+1] = true;
}
lab[2*i][2*j] = true;
lab[2*i+1][2*j+1] = false;
int rd = RAND()%2;
if(rd && (part[m*i + j] != part[m*(i+1) + j])) //Si on essaie de faire un trou dans (2*i+1, 2*j+1), (2*i+3, 2*j+1)
{
unite(part, m*i + j, m*(i+1) + j, n/2 * m/2);
lab[2*(i+1)][2*j+1] = false;
}
else lab[2*(i+1)][2*j+1] = true;
rd = RAND()%2;
if(rd && (part[m*i + j] != part[m*i + j+1])) //Si on essaie de faire un trou dans (2*i+1, 2*j+1), (2*i+1, 2*j+3)
{
unite(part, m*i + j, m*i + j+1, n/2 * m/2);
lab[2*i+1][2*(j+1)] = false;
}
else lab[2*i+1][2*(j+1)] = true;;
}
}
return lab;
}
void print_lab(bool** lab, int n, int m)
{
for(int i = 0; i<n; i++)
{
for(int j = 0; j<m; j++)
{
if(lab[i][j]) printf("X");
else printf(" ");
}
printf("\n");
}
}
void free_lab(bool** lab, int n)
{
for(int i = 0; i<n; i++)
{
free(lab[i]);
}
free(lab);
return;
}
int main(void)
{
//eratosthene(256);
//printf("%d\n", eratosthene(256));
//int i = 0;
//printf("%f\n", read_float("98456,97845afdeaeafez", &i));
bool** lab = genere_laby(15, 15);
print_lab(lab, 15, 15);
free_lab(lab, 15);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
typedef struct cc {
int val; //Ordre croissant
struct cc* next;
} chaine;
bool est_premier(int n, chaine* t){
chaine* prem = t;
int root = sqrt( (float)n ) + 1;
bool b = true;
while( prem != NULL && prem->val <root && b){
b = (n % prem->val != 0);
prem = prem->next;
}
return b;
}
chaine* init_chaine(int n){
chaine* new = malloc(sizeof(chaine));
new->val = n; new->next = NULL;
return new;
}
void add(int n, chaine* t){
chaine* new = init_chaine(n);
chaine* tmp = t;
while (tmp->next !=NULL) {tmp = tmp->next;}
tmp->next =new;
}
chaine* erastot( int n){
chaine* start = malloc(sizeof(chaine));
start->val = 2;
for(int i = 3; i< n+1; i++){
if (est_premier(i, start)){
add(i, start);
}
}
return start;
}
int somme(int tableau[], int taille){
int acc = 0;
for (int i = 0; i<taille; i++){
acc += tableau[i];
}
return acc;
}
void calc_somme(char* s ){
float c_t_f(char* s, int n){
assert(n>0);
char* nw = malloc(sizeof(char)*(n+1)); nw[n] = '\0';
for(int i = 0; i<n; i++){
nw[i] = s[i];
}
return atof(nw);
}
int i = 0;
chaine* symbols = init_chaine(-1);
while(s[i] != '\0'){
if( s[i] == '+' || s[i] == '-'){
add(i,symbols);
}
i++;
}
add(i,symbols);
float somme = c_t_f(s, symbols->next->val - symbols->val -1);
chaine* tmp = symbols->next;
while( tmp->next != NULL){
if( s[tmp->val] == '+'){
somme += c_t_f(&(s[tmp->val +1]), tmp->next->val - tmp->val -1);
}
else{
somme -= c_t_f(&(s[tmp->val +1]), tmp->next->val - tmp->val -1);
}
tmp = tmp->next;
}
printf("La somme est %f\n",somme);
}
//int** labyrinthe(int n, int m){
//}
int main(){
int notes[5] = {12,18,15,13,20};
int total = somme(notes,5);
printf("Le total est %d. \n", total);
char buffer[100];
scanf("%s", buffer);
calc_somme(buffer);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int somme(int tableau[], int taille){
int accumulateur;
accumulateur = 0;
for (int i=0; i<taille; i++){
accumulateur = accumulateur + tableau[i];
}
return accumulateur;
}
void afficher(int* tableau, int taille){
printf("{");
for (int i=0; i<taille-1; i++){
printf("%d, ", tableau[i]);
}
printf("%d}\n",tableau[taille-1]);
}
void eratostene(int n){
int * premiers = malloc(sizeof(int)*(n+1));
int acc;
acc =0;
for(int i=0;i<n+1;i++) premiers[i] = i;
for(int i=2;i<n+1;i++){
if (premiers[i] == i){
printf("%d ;",i);
acc++;
for(int k=2;(i)*k<=n;k++){
premiers[(i)*k] = 0;
}
}
}
printf("\n pi(%d) = %d. \n",n,acc);
free(premiers);
}
void calc(void){
char buffer[100];
printf("Entrez une ligne de calcul:\n");
scanf("%[^\n]s",&buffer);
bool b = true;
int cursor = 0;
int res = 0;
int dec = 0;
bool mode = true;
while(buffer[cursor] != '\0'&&b){
int compt = 0;
int acc = 0;
int i = buffer[cursor] - '0';
while(0 <= i && i <= 9){
acc *= 10;
acc += i;
cursor++;
i = buffer[cursor] - '0';
compt++;
}
if (buffer[cursor] == '+'&&compt>0){
cursor++;
if (mode) res += acc;
else dec +=acc;
mode = true;
}
else if(buffer[cursor] == '.'&&mode){
res += acc;
mode = false;
}
else if(buffer[cursor] == '\0'){
if (mode) res += acc;
else dec +=acc;
}
else{
b = false;
}
}
if (b||buffer[cursor] == '\0'){
printf("%d.%d\n",res,dec);
}
else{
printf("Bye.");
}
}
int main(){
/*
int notes[5] = {12, 18, 15, 13, 20};
int total = somme(notes, 5);
printf("le total est : %d. Au revoir. \n", total);
afficher(notes, 5);
eratostene(100);
*/
calc();
return 0;
}
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
void premier (bool* tab, int n){
int i=2;
while (i<n+1)
{
if (tab[i])
{
for (int k = 2; i*k <= n; k++)
{
tab[k*i]=false;
}
}
i++;
}
}
int pi (int n){
bool* tab=malloc(sizeof(bool)*(n+1));
for (int i = 0; i < n+1; i++)
{
tab[i]=true;
}
premier(tab, n);
int acc=0;
for (int i = 2; i < n+1; i++)
{
if (tab[i])
{
acc++;
}
}
free(tab);
return acc;
}
void eratosthene (int n){
bool* tab=malloc(sizeof(bool)*(n+1));
for (int i = 0; i <= n; i++)
{
tab[i]=true;
}
premier(tab, n);
for (int i = 2; i <= n; i++)
{
if (tab[i])
{
printf("%d est premier\n",i);
}
}
free(tab);
}
int main(){
eratosthene(100);
printf("%d nb premier\n",pi(100));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int somme(int tableau[], int taille) {
int i;
int accumulateur;
accumulateur = 0;
for (i= 0; i < taille; i++) {
accumulateur = accumulateur + tableau[i];
}
return accumulateur;
}
void pi(int n) {
int tableau[n];
for(int i = 0; i<n; i++){
tableau[i] = 0;
}
for (int i=2; i <n ; i++) {
for (int j = 2; j*i<=n; j++){
tableau[j*i] = 1;
}
}
int compteur = 0;
for (int i=2; i <n ; i++){
if (tableau[i] == 0) {
printf("%d\n", i);
compteur = compteur + 1;}
}
printf("%d",compteur);
}
void calc_sum(char* s){
int plus = 0;
for (int i = 0; s[i] != 0; ++i) {
}
}
int main(){
int notes[5] = {12,18,15,13,20};
int total = somme(notes, 5) ;
printf("le total est : %d. AU revoir. \n", total);
pi(100);
return 0;
}
// Retourne dans (x2, y2) les coordonnées d'une case voisine à (x,y) qui n'a pas déjà été explorée
void find_next_direction(int* x2, int* y2, int x, int y, int n, int m, bool** filled) {
// DIRECTION :
//
// ^
// |
// |
// 1
// <-- 0 2 -->
// 3
// |
// |
// v
while(true){
int dir = rand() % 4;
switch(dir) {
case 0:
*x2 = x - 1;
*y2 = y;
break;
case 1:
*x2 = x;
*y2 = y - 1;
break;
case 2:
*x2 = x + 1;
*y2 = y;
break;
case 3:
*x2 = x;
*y2 = y + 1;
break;
}
if(!is_filled(n,m,*x2,*y2,filled)) break;
}
}
#include <stdio.h>
#include <stdbool.h>
int somme(int tableau[], int taille){
int i;
int accumulateur;
accumulateur = 0;
for (i = 0; i < taille; i++){
accumulateur = accumulateur + tableau[i];
}
return accumulateur;
}
void affiche(int tableau[], int taille){
int i;
for(i = 0; i < taille; i++){
printf("%d \n", tableau[i]);
}
}
int main(){
int notes[5] = {12,18,15,13,20};
int total = somme(notes, 5);
affiche(notes, 5);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
void afficheLabyrinthe(int n, int m, bool A[][m]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A[i][j]) printf("X");
else printf(" ");
}
printf("\n");
}
}
void creerLabyrinthe(int n, int m) {
bool A[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || i == n-1 || j == 0 || j == m-1) {
A[i][j] = true;
} else if (i % 2 == 0 && j % 2 == 0) {
A[i][j] = true;
} else if (i % 2 == 1 && j % 2 == 1) {
A[i][j] = false;
} else {
A[i][j] = false;
}
}
}
afficheLabyrinthe(n, m, A);
}
int main(void) {
int n = 21;
int m = 21;
creerLabyrinthe(n, m);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/*
*Cette fonction effectue le crible d'eratosthene sur un tableau
*@return nothing
*/
int eratosthene(int size, int isPrime[]){
for (int i=2; i<size; i++){
for (int j = 2*i;j<size; j+=p){
isPrime[j] = -1
}
}
}
return isPrime[]
}
/* cete fonction affiche le tableau donné en argument
*@return nothing
*/
void affiche(int isPrime[])
{ for (i=0;i<size;++){
if isPrime[]=0;
printf(i)
}
}
/*
*cette fonction initialise le tableau isPrime en attribuant la valeur 1 par defauts à tous les entiers sauf 0 et 1 dont on sait qu'ils sont premiers
*@return nothing
*/
void init(char isPrime[],int size){
isPrime[0]=isPrime[1]=0
for (i=2;i<size;i++) {
isPrime[i] = 1
}
}
int main(int argc,char* *argv)
{
int size = atoi(arrgv[1]);
int isPrime[size];
init(isPrime)
eratosthene(size,isPrime)
affiche(isPrime)
}
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
void premier (bool* tab, int n){
int i=2;
while (i<n+1)
{
if (tab[i])
{
for (int k = 2; i*k <= n; k++)
{
tab[k*i]=false;
}
}
i++;
}
}
int pi (int n){
bool* tab=malloc(sizeof(bool)*(n+1));
for (int i = 0; i < n+1; i++)
{
tab[i]=true;
}
premier(tab, n);
int acc=0;
for (int i = 2; i < n+1; i++)
{
if (tab[i])
{
acc++;
}
}
free(tab);
return acc;
}
void eratosthene (int n){
bool* tab=malloc(sizeof(bool)*(n+1));
for (int i = 0; i <= n; i++)
{
tab[i]=true;
}
premier(tab, n);
for (int i = 2; i <= n; i++)
{
if (tab[i])
{
printf("%d est premier\n",i);
}
}
free(tab);
}
int main(){
eratosthene(100);
printf("%d nb premier\n",pi(100));
return 0;
}