Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement