Thread: listing twin prime numbers help!

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    listing twin prime numbers help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    { int firstnum,secondnum;
    
         for(firstnum=1; firstnum<=499; firstnum++)
         {
           if(secondnum%firstnum)
               {
                secondnum=(firstnum*2)+1;
                printf("%d ",secondnum);
               }
           else
               {
                    printf("done");
               }
          }
          system("PAUSE");
          return 0;
    }
    need help, i cant seem to find out how to list all the prime numbers from 1-1000...im not sure how to test it to check if its a prime number this code prints 9 for example but 9 is not prime, can some1 help me fix it?? i also edited this to look like this, which is better:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    { int firstnum=1,secondnum=3;
    
    
          if(!(secondnum%firstnum))
          {
              for(firstnum=1; firstnum<=499; firstnum++)
              {
                secondnum=(firstnum*2)+1;
                printf("%d ",secondnum);
              }
          }
          else
              {
                printf("Doesn't work MANG!!!");
              }
          system("PAUSE");
          return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well using the web to search out the prime number algorithms (eg sieve of eratosthenes) would be a start.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Remember this? A prime is only dividable by itself and 1.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  3. More Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 3
    Last Post: 02-21-2003, 10:06 AM
  4. Prime numbers
    By Lazy Student in forum C Programming
    Replies: 12
    Last Post: 05-14-2002, 05:53 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM