Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
150113_CribaErastotenes
//Title of this code //gcc 4.8.2 #include <stdio.h> #include <math.h> short asignarPrimosAlArray(long d, long a[d]); void mostrarArray(long d, long a[d]); void cribaErastotenes(long d, char c[d]); #define _D 100000 int main(void) { long primos[_D]; short valorRespuesta = asignarPrimosAlArray(_D, primos); if(valorRespuesta == 0) { mostrarArray(_D, primos); } else { printf("Agh!!!!!: No tengo suficientes primos...\n"); } return 0; } short asignarPrimosAlArray(long d, long a[d]) { long dim = 1000000; long i, j; char erastotenes[dim]; cribaErastotenes(dim, erastotenes); for(i = 1 , j = 0 ; j < d && i < dim ;i++) { if(erastotenes[i] == 'p') { a[j++] = i; } } if(i == dim) return -1; return 0; } void mostrarArray(long d, long a[d]) { long i; for(i = 0 ; i < d ; i++) { if(i % 10 == 0) printf("\n"); printf("%7ld", a[i]); } return; } void cribaErastotenes(long d, char c[d]) { long i, j; for(i = 1 ; i < d ; i++) { c[i] = 'p'; } for(i = 2 ; i <= sqrt(d) ; i++) { if(c[i] == 'p') { for(j = 2 * i ; j < d ; j += i) { c[j] = 'c'; } } } }
run
|
edit
|
history
|
help
0
HW22
Programação em C, testes
Memory leak
arrows
joseph a triangle
Practica 5 fibonacci
Simple interest
gets.c
Assignment 7
Largest and smallest