#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#define max 20
#define m_size 10
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return 1;
else
return 0;
}
int is_First2(int value)
{
int temp_value, *tab_Values;;
int i,j;
temp_value=0;
tab_Values=(int*)calloc(max,sizeof*tab_Values);
i=0;
j=2;
do
{
if(is_First(j))
{
tab_Values[i]=j;
++i;
}
++j;
}while(i<max);
for(i=0;i<max;i++)
{
if(value==i)
temp_value=tab_Values[i];
}
free(tab_Values);
return temp_value;
}
int is_Fib(int value)
{
if(value<3)
return 1;
else
return is_Fib(value-2)+is_Fib(value-1);
}
int is_Luc(int value)
{
if(value==1)
return 2;
if(value==2)
return 1;
else
return is_Luc(value-1)+is_Luc(value-2);
}
struct int_parameters
{
int value;
int size;
};
void *write_n(void *temp_par)
{
struct int_parameters *temp_struct=(struct int_parameters*) temp_par;
int i;
for(i=0;i<temp_struct->size;++i)
fputc(temp_struct->value,stderr);
return NULL;
}
int main(int argc, char **argv)
{
int i,rand_first,rand_fib,rand_luc;
pthread_t thread_first;
pthread_t thread_fib;
pthread_t thread_luc;
struct int_parameters first_parameters;
struct int_parameters fib_parameters;
struct int_parameters luc_parameters;
srand(time(NULL));
rand_first=rand()%max;
rand_fib=rand()%max;
rand_luc=rand()%(max-3)+2;
first_parameters.value=is_First2(rand_first);
first_parameters.size=m_size;
pthread_create(&thread_first,NULL,&write_n,&first_parameters);
fib_parameters.value=is_Fib(rand_fib);
fib_parameters.size=m_size;
pthread_create(&thread_fib,NULL,&write_n,&fib_parameters);
luc_parameters.value=is_Luc(rand_luc);
luc_parameters.size=m_size;
pthread_create(&thread_luc,NULL,&write_n,&luc_parameters);
pthread_join(thread_first,NULL);
pthread_join(thread_fib,NULL);
pthread_join(thread_luc,NULL);
return 0;
}
Brak komentarzy:
Prześlij komentarz