#include <stdio.h>
#include <time.h>
#define N 20
time_t start,finish;
int isprime(int ), search(int B[80][3], int l, int TOP), A[80][3];
int k,init_prime,next_prime,run,number,in_pr,found,top,column;
double duration;
main()
{
start=time(0);
init_prime=3; // oi 2 prwtoi pou anamesa tous vriskontai oi sunthetoi
next_prime=number=in_pr=0;
run=init_prime+2;
k=1;
for ( top=0;top<80;top++)
{
for ( column=0;column<3;column++)
{
A[top][column]=0;
}
}
top=column=0;
while ( k<= N)
{
found=search(A[80][3],k,top);
if (found+1)
{
printf("First %d consecutive composites from %d to %d\n",A[found][0],A[found][1],A[found][2]);
k+=2;
}
else
{
if (isprime(run))
{
next_prime=run;
number=next_prime-init_prime-1;
in_pr=init_prime;
init_prime=next_prime;
if ( number==k)
{
printf("First %d consecutive composites from %d to %d\n",number,in_pr,next_prime);
k+=2;
}
else if ( number>k)
{
if ( search(A[80][3],number,top)+1)
{
top++;
A[top][0]=number;
A[top][1]=in_pr;
A[top][2]=next_prime;
}
run+=2;
}
else
{
run+=2;
}
}
}
}
finish=time(0);
duration=difftime(finish,start);
printf("Program was executed in %lf seconds\n",duration);
return 0;
}
//sunarthsh isprime()-->epistrefei an enas arithmos, perittos kai megaluteros tou 3, einai prwtos
int isprime(int run)
{
int i=3;
int done=1;
while (done==1 && i*i<=run)
{
if (run % i==0)
{
done=0;
break;
}
i+=2;
}
return done;
}
//sunarthsh search()-->exetazei an uparxei ston pinaka to stoixeio pou psaxnw
int search ( int B[80][3], int l, int TOP)
{
int j;
for (j=0;j<=TOP;j++) //searching..
{
if (B[j][0]==l) // an nai epistrefei thn thesh ston pinaka
{
return j;
break;
}
}
if (j==(TOP+1)) // an oxi -1
return -1;
}