/*

	This program finds all the prime numbers less than the
	given uper limit by the method of the Pythagorean Sieve.
        The maximum prime number and the number of times to repeat
        the loop interation (i.e. find the same primes n times)
        must be input by the user

	83 Jan 19	v 1.0	Tom Perrine
*/

#define MAXINT 10000
#define MAX 80
long time();

long t_start, t_end;
long total_time  = 0;
int list[MAXINT];
char gfiden [] = "PRIMEU";
main(argc,argv)
int argc;
char *argv[];
{
	char line[80];
	char * l = line;
	register int i;
	int repeat_for = 0;
	int num_primes = 0;
	printf ("ENTER THE MAXIMUM NUMBER FOR THE PRIME GENERATION (10000 is max):");
	getline(l,MAX);
	num_primes = atoi(line);
	printf ("ENTER THE NUMBER OF TIMES TO REPEAT:");
	getline(l, MAX);
	repeat_for = atoi(line);
	timeon();
	for (i = 0; i < repeat_for; i++)
		primeinner(num_primes);
	timeoff();
	exit();

}

getline(s, lim)
	char *s;
int lim;
{
	int c,i;
	char *save;
	save = s;
	for (i=0; i<lim-1 && (c=getchar()) != 
		'\0' && c != '\n'; *save++ = c, ++i)
			;	
		if ((c  ==  '\0') && (i == 1))
			--save;
		*save = '\0';
		return(i);
}

primeinner(high)
register int high;
{
register int i,j;


for (i=0; i <= high; i++)
	list[i]=i;

for (i=2; i<= high; i++)
	{
	while (list[i] == 0)
		i++;
	for (j=2*i; j <= high; j+=i)
		list[j]=0;
	}
}

timeon() {
	t_start = time(0);
	printf("PRIME test starting: %s\n", ctime(&t_start));
}

timeoff(){
	t_end = time(0);
	total_time = t_end - t_start;
	/***(65536 * (t_end[0] - t_start[0])) + ((t_end[1] - t_start[1])); **/
	printf("PRIME test ending: %s\n", ctime(&t_end));
	printf("total time = %d\n", total_time);
}
