Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- int x=1000000; //default 1 million primes
- if (argc > 1) x = (int)(strtol(argv[1], NULL, 10));
- printf ("Program will calculate %i prime",x); if(x!=1) printf("s"); printf(". Size of int is %lu bytes. Tap enter to start allocating memory.\n",sizeof(int));
- getchar();
- int howmanyprimes=x-1;
- unsigned int* primes=calloc(howmanyprimes, sizeof(int));
- int iscomposite = 0;
- primes[0] = 3;
- unsigned int index = 0;
- if (x==0)
- {
- return 0;
- }
- if (x==1)
- {
- printf ("2 \nTap enter key to exit.");
- getchar();
- return 0;
- }
- printf ("2 ");
- unsigned int n = 0;
- for (n=3;;n+=2)
- {
- iscomposite=0;
- unsigned int n2 = 0;
- for (n2=0;primes[n2]*primes[n2]<=n;n2++)
- {
- if ((n%primes[n2])==0)
- {
- iscomposite = 1;
- break;
- }
- }
- if (iscomposite==0)
- {
- index++;
- primes[index]=n;
- printf ("%i ",n);
- }
- if (index>howmanyprimes-1) break;
- }
- printf("\nTap enter key to exit.");
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement