Commit 0e3aac2defe8a4dd2e01d9e0b4f39463cba40d5b
1 parent
070a61d4
ansi fait
Showing
3 changed files
with
52 additions
and
0 deletions
Show diff stats
No preview for this file type
... | ... | @@ -0,0 +1,24 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdint.h> | |
3 | +#include <stdlib.h> | |
4 | +#include <stdbool.h> | |
5 | +#define erreur(...) { fprintf(stderr,__VA_ARGS__) ; exit(EXIT_FAILURE); } | |
6 | +int32_t premier(uint32_t n){ | |
7 | +bool crible[n*n+1]; | |
8 | +for(uint32_t i=0;i<=n*n;i++) crible[i]=true; | |
9 | +for(uint32_t i=2;i<=n;i++) | |
10 | + if(crible[i]){ | |
11 | + uint32_t j=i*i; | |
12 | + while(j<=n*n){ crible[j]=false; j += i; } | |
13 | + } | |
14 | +int32_t result=-1; | |
15 | +for(int32_t i=n*n;i>=0;i--) if(crible[i]){ result=i; break; } | |
16 | +return result; | |
17 | +} | |
18 | +int main(void){ | |
19 | +uint32_t n; | |
20 | +if(scanf("%d",&n)!=1) erreur("Echec de lecture du nombre\n"); | |
21 | +printf("premier -> %d\n",premier(n)); | |
22 | +return 0; | |
23 | +} | |
24 | + | ... | ... |
... | ... | @@ -0,0 +1,28 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdint.h> | |
3 | +#include <stdlib.h> | |
4 | +#include <stdbool.h> | |
5 | +#define erreur(message) { fprintf(stderr,message) ; exit(EXIT_FAILURE); } | |
6 | +int premier(int n){ | |
7 | +int taille=n*n+1; | |
8 | +int result=-1; | |
9 | + int *crible = malloc( taille * sizeof(int) ); | |
10 | +int i; | |
11 | +for(i=0;i<=n*n;i++) crible[i]=true; | |
12 | +for(i=2;i<=n;i++) | |
13 | + if(crible[i]){ | |
14 | + int j=i*i; | |
15 | + while(j<=n*n){ crible[j]=false; j += i; } | |
16 | + } | |
17 | +for(i=n*n;i>=0;i--) if(crible[i]){ result=i; break; } | |
18 | +return result; | |
19 | +} | |
20 | + | |
21 | +int main(void){ | |
22 | +int n; | |
23 | +if(scanf("%d",&n)!=1) erreur("Echec de lecture du nombre\n"); | |
24 | +printf("premier -> %d\n",premier(n)); | |
25 | +return 0; | |
26 | +} | |
27 | + | |
28 | + | ... | ... |