Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Prime Number Generator

DZone's Guide to

Prime Number Generator

·
Free Resource
This program will calculate & list the first n primes, where n is a number specified on the command line or (if none is specified) 100.  n is only limited by the size of ints on your platform.

#include 
  
   
#include 
   
    
#include 
    
     

int main(int argc, char** argv) {
 int qty = (argc > 1) ? (int) strtol(argv[1], NULL, 10) : 100;
 if (qty < 3) qty = 100;
 unsigned int primes[qty];
 primes[0] = 2U; primes[1] = 3U;
 printf("2\n3\n");
 for (int i=2; i
     
       bound) break; /*Not a viable shortcut for small quantities*/
   if (!(j % primes[k])) goto iter;
  }
  primes[i] = j;
  printf("%u\n", j);
 }
 return 0;
}

     
    
   
  
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}