[NBLUG/talk] OT: First 500 prime numbers

Rob Orsini orsini at sonic.net
Thu Jun 5 16:27:01 PDT 2003


At 04:12 PM 6/5/2003 -0700, you wrote:
>Once more, with feeling  ;-)   :
> > > I'm trying to make a program in C that prints out a table
> > > of the first 10 prime numbers, in an endeavor to eventually
> > > create a program that prints out the first 500 prime numbers
> > > (Knuth, v1, p. 147).  So far I am without success.  The file is

Here's what I came up with.  Seems to work although I'm not sure if it's 
the empirical (fastest,cleanest) solution :-)

/*
  * prime.c -- a failed attempt to print a table of first ten primes
  * NOTE: This program just prints out 3 4 5 6 7 8 9 10 11 12
  * To nblug folks: please help, if you can.
  */

#include <stdio.h>

int main()
{
         int i;
         int num = 3;
         int prime_counter = 0;
         int prime[10];
         int is_prime;

         while (num < 10)
         {

           is_prime=1;

                 for (i = 2; i < num; i++)
                 {
                         if ((num % i) == 0)
                         {
                           is_prime=0;
                                 break;
                         }
                         else
                                 continue;
                 }

                 if ( is_prime==1 )
                 {
                   prime[prime_counter] = num;
                   prime_counter++;
                 }

                 num++;
         }

         for (i = 0; i < prime_counter; i++)
                 printf("%d ", prime[i]);

         printf("\n");

         return 0;
} 




More information about the talk mailing list