#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#define END 4000000

unsigned int xb=3, xt=4000000;
unsigned int primetbl[2000000], primes=0; 
int ipp=0;
int test(unsigned int testx);
int test2(unsigned int foo);
void other_end(void *);
void foo();

void main()
{
	foo();
	do
	{
		if(xb<2100)
		{
			if(test(xb)==0) 
			{
				printf("%u\n", xb);
				primetbl[primes]=xb;
				primes++;
			}
		}
		else
		{
			if(test2(xb)==0) printf("%u\n", xb);
		}
		xb+=2;
	}while(xb<2000000);
	printf("\nDone\n");
	exit(0);
}

void foo()
{
	pthread_t test_thread;
	pthread_create(&test_thread, NULL, (void *)&other_end, (void *)NULL);
}

void other_end(void *ptr)
{
	unsigned int same=3999999;
	printf("\nThread started\n");
	while(same>2000000)
	{
		if(test(same)==0)
		{
			/* printf("- %u\n", same); */
			fprintf(stdout, "%u\n", same);
		}
		same-=2;
	}
	printf("\nThread: Done\n");
}

int test(unsigned int testx)
{
	int root, i, divnum;
	/* char str[20], str2[20], ch; */

	if(testx%2==0){
		divnum=2;
		return 1;
	}
	/* Another dirty fix */
	if(testx==3 || testx==5) return 0;
	root=sqrt(testx);
	divnum=3;

	do{
		if(testx%divnum==0) return 1;
		divnum+=2;
	}while(divnum<=root);
	return 0;
}

/*
void hyper(void *ptr)
{
	char string1[20], string2[1];
	int x1=0, a1=0, b1, y1;
	unsigned int flapp;
	flapp=x;	
	sprintf(string1, "%i", flapp);
	y1=strlen(string1);
	while(x1<=y1){
		sprintf(string2, "%c", string1[x1]);	
		b1=atoi(string2);
		a1=b1+a1;
		x1++;
	}
	if(test(a1)==0) printf("h");
}
*/
int test2(unsigned int foo)
{
	int foodiv=3, fooroot, fooprime=0;
	fooprime=0;
	fooroot=sqrt(foo);
	/* if(foo%3==0) return 0; */
	while(primetbl[fooprime]<=fooroot)
	{
		if(foo%primetbl[fooprime]==0) return 1;
		fooprime++;
	}
	return 0;
}
