12.07.2015 Views

Hilos - Universidad Nacional de San Antonio Abad del Cusco

Hilos - Universidad Nacional de San Antonio Abad del Cusco

Hilos - Universidad Nacional de San Antonio Abad del Cusco

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UNIVERSIDAD NACIONAL DE SAN ANTONIO ABAD DEL CUSCOSISTEMAS DE OPERACIONES IGUIA DE LABORATORIOecp 6 <strong>de</strong> 8Solución/**/Nombre : primo.cObjetivo : calcula el N-esimo número primoAutor : Edwin CarrascoFCreacion : 06/12/2007FModificacion :#inclu<strong>de</strong> #inclu<strong>de</strong> /* Calcula numeros primos sucesivos. Devuelve el N-esimo numero primo,en don<strong>de</strong> N es el valor apuntado por *arg. */void* calcula_primo (void* arg){int candidato = 2;int n = *((int*) arg);while (true){int factor;int es_primo = 1;/* Prueba la primalidad mediante divisiones sucesivas. */for (factor = 2; factor < candidato; ++factor){if (candidato % factor == 0){es_primo = 0;break;}}}/* Es el numero primo que estamos buscando? */if (es_primo){if (--n == 0){/* Devuelve el numero primo <strong>de</strong>seado como el valor <strong>de</strong>retorno <strong>de</strong>l hilo. */return (void*) candidato;}}++candidato;}return NULL;int main (){pthread_t hilo;int que_primo = 5000;int primo;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!