#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <pthread.h>

#define INTERVALO 300
#define BEGINIG 5000

void *thread_func(void *arg);

int main(int argc, char *argv[])
{
    
  uint32_t quantidade, i;
  pthread_t *threads;
  uint32_t *indice;

  if (argc==2) quantidade = atoi(argv[1]);
  else quantidade = 20;
  
  threads = (pthread_t*) malloc (quantidade*sizeof(pthread_t));
  indice = (uint32_t*) malloc (quantidade*sizeof(uint32_t));

  for(i=0; i < quantidade; i++) {
    indice[i] = i;
    pthread_create(&(threads[i]), NULL, thread_func, &(indice[i]));
  }
  
  for(i=0; i < quantidade; i++) {
    pthread_join(threads[i], NULL);
  }
  return 0;
  
}

void *thread_func(void *arg) {
  uint32_t *k = (uint32_t *)arg;  
  
  uint32_t inicio, fim, j, primo, i, total;
  total = 0;
  inicio = (*k) * INTERVALO + BEGINIG;
  fim = inicio + INTERVALO;
  for ( i = inicio; i < fim; i++){
  	primo=0;
  	for ( j = 2; j <= i; j++){
  	  if ( i%j == 0 ){
  		primo++;
  		if ( primo > 1 ){
  		  j=i;
  		}
  		else{
  		  if ( primo == 1 && j == i ){
  			total++;
  		  }
  		}
  	  }
  	}
  }
  return NULL;
}