CCCPaste

Primes C (Working)

#include 
#include 
#include 
#include 

#define TRUE 1
#define FALSE 0

int main(int argc, char* argv[])
{
	int n, m, lim;
	double s;
	char* nums;
	char input[255];
	clock_t start, stop;

	if (argc == 2)
        lim = atoi(argv[1]);
    else
    {
        printf("Enter a limit for this search (empty for default): ");
        gets(input);
        if (input[0])
            lim = atoi(input);
        else
            lim = 100000000;
    }

	s = sqrt(lim);

	start = clock();

	nums = (char *)calloc(lim + 1, sizeof(char));

	for (n = 2; n < s; n++)
	{
		if (nums[n] == FALSE)
		{
			m = n * n;
			while (m <= lim)
			{
				nums[m] = TRUE;
				m += n;
			}
		}
	}

	stop = clock();

	printf("It took %f seconds.\n", (double)(stop - start) / 1000);
	printf("Do you want to list the results, count total, or do nothing (L/C/N)? ");
	gets(input);
	if (input[0] == 'L' || input[0] == 'l' || input[0] == 'C' || input[0] == 'c')
	{
		m = 0;
		for (n = 2; n < lim; n++)
		{
			if (nums[n] == FALSE)
			{
			    if (input[0] == 'L' || input[0] == 'l')
                    printf("%d\n", n);
				m++;
			}
		}
		printf("Total primes = %d\n\n", m);
		printf("Press ENTER to exit. . . ");
		getchar();
	}

	free(nums);
	return 0;
}