Thread: Program to store 1000 prime numbers in an array and print out every 15th

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Program to store 1000 prime numbers in an array and print out every 15th

    This is a program to put the first 1000 numbers in an array and print out every 15th. It compiles but the program crashes.

    Code:
    #include <stdio.h> 
    int main ()
    {
    int array[1000];
     
     
    int a=0;
    int num1=2;
    int num2=0; 
    int b = 0;
         
    printf("These are every 15th prime number up to the 1000th prime:\n\n");
         
        while (b < 1000){
             
                         
        while ( num1 < b ){
     
     
            if ( b%num2 == 0){
                             
            num2++;
    }
                     
            num1++;
        }   
                     
            if (num2 == 0){
                     
                     
                array[b] = b;
    }
                b++;
    }
    b=0;    
    
    
        while (b<=1000){
         
        printf("%d\n",array[b]);
         
        b+15;
         
        }
             
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would first find every prime number you need, and then use an if statement ONLY in the printing block of code, to allow printing every 15th number in the prime array. In your printing loop:

    Code:
    if(i % 15==0 && i) 
       printf("%d", primes[i]);
    In general, while() loops are not as good when you're working out logic for a program. For loops just set your mind right to solve the looping problems.

    Code:
       Handle the two prime number in here, 
       since it's the only even prime number.
    
    
    for(primeCnt =0;primeCnt<1000;primeCnt++) {
       
       for(testNum = 3,flag=1;;testNum+=2) { //skip the even numbers
                       
          for(n=3;n*n<=testNum;n+=2) { //start the testing loop
           with the modulus operator % if the answer is ever 0
           then testNum is a composite number, so break out of 
           the test loop, after setting a flag to 0.
    
          }
          if a number has a flag of 1 still, then it's a prime 
          number. Check your flag.
       }
    }
    Too much going on here to use while loops comfortably. I know you can do it, but there's a reason why for loops are the most popular kind of loop.

    This is the "trial by division" algorithm. There are others, like the Sieve of Eratosthenes, which are even faster. Wiki has info on them all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. Hi, Can anyone help me? print prime numbers.
    By kelai in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 01:35 PM
  3. Replies: 11
    Last Post: 06-23-2010, 01:36 AM
  4. Prime Numbers and Array...
    By Deux in forum C Programming
    Replies: 12
    Last Post: 12-20-2004, 04:12 PM
  5. C Program for Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 12
    Last Post: 02-19-2003, 04:55 AM