Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #define PRIMELISTSIZE 100000
- int uppercheck = 0;
- int is_prime(int c, int m, int *primes) {
- int i, p;
- for (i = 0; i <= m; i++)
- {
- p = primes[i];
- if (c % p == 0)
- {
- return(1);
- }
- }
- return(0);
- }
- int main(int argc, char **argv) {
- int i, square;
- int curListLength = 1;
- int primelistsize = PRIMELISTSIZE;
- int opt;
- // put ':' in the starting of the
- // string so that program can
- //distinguish between '?' and ':'
- while((opt = getopt(argc, argv, "m:")) != -1)
- {
- switch(opt)
- {
- case 'm':
- primelistsize = atoi(optarg);
- break;
- case ':':
- printf("option needs a value\n");
- break;
- }
- }
- // optind is for the extra arguments
- // which are not parsed
- for(; optind < argc; optind++){
- printf("extra arguments: %s\n", argv[optind]);
- }
- int primes[primelistsize];
- primes[0] = 2;
- int candidate = 3;
- square = primes[uppercheck] * primes[uppercheck];
- while (curListLength < primelistsize)
- {
- if (square < candidate) {
- uppercheck++;
- square = primes[uppercheck] * primes[uppercheck];
- }
- if (is_prime(candidate, uppercheck, primes) == 0)
- {
- primes[curListLength] = candidate; curListLength++;
- }
- candidate += 2;
- }
- for (i = 0; i < primelistsize; i++)
- printf("%d\n", primes[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement