[NBLUG/talk] OT: First 500 prime numbers

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


Opps!  Sorry, that was all the primes under 10.  THIS is what you wanted:

/*
  * 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 (prime_counter < 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;
}




At 04:28 PM 6/5/2003 -0700, you wrote:
>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;
>}
>_______________________________________________
>talk mailing list
>talk at nblug.org
>http://nblug.org/mailman/listinfo/talk




More information about the talk mailing list