how do i calculate prime number

This is a discussion on how do i calculate prime number within the C Programming forums, part of the General Programming Boards category; Why not just delete that line of code? It's unnecessary and a nuisance. I've dug up an old laptop I ...

  1. #46
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why not just delete that line of code? It's unnecessary and a nuisance.

    I've dug up an old laptop I can try this on. If it will boot!
    Last edited by Adak; 01-12-2008 at 07:39 AM.

  2. #47
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Code:
    #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int arrayofprimes[100];// create space for array
         int top,remainder,numberofprimes,prime,i;  
         int minusONE;//pointer for array
    
    
     
       
       printf("pls enter top limit:");
       scanf("%d",&top);
    
         prime = 1; 
         numberofprimes = 0;
    
    
       do
       {
          if(top > 5) 
              {
              minusONE = top / 2;
              }
          else
             { 
               minusONE = top - 1;
             }
    
    
           for(minusONE; minusONE > 1; minusONE--) 
     
                {
                remainder = top % minusONE;
                     if(remainder==0)  //it's not a prime number, so stop processing it.
                    {
                        prime = 0;
                        break;  //break out of the for loop.
                    }
            } //end of for loop
                   
            if(prime==1) 
                {
                    arrayofprimes[numberofprimes++] = top;
                }
                
                                       
             prime=1;
             }while(top-- >1);
         
           
          for(i = 0; i < numberofprimes; i++)
         {
              printf("\nthe prime number in array[%02d]is %d ",i,arrayofprimes[i]);
         }
          printf("\n");
    
    
         system("pause");
         return 0;
       
         
     }
    this is the code i have.. and it work.. can explain how those line work ?so i hav better view (=

  3. #48
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The only tricky thing about it is the looping logic, once you know how to work with the mod operator.

    It's a one to many relationship - you have a number, and you have to test it against a whole bunch of other numbers, (and that's the nested inside loop).

    Then you finish that one number, and have to repeat it with another number. (that's the outer loop's job to keep feeding the inner loop with the next number to test).

    If ANY test of mod on a number == 0, then the number isn't a prime, so you need that "prime" variable to flag and show the first time that happens, so the test on that one number can break off. That's the break statement in the inner for loop.

    I'm off the board for most of the rest of today. You've still got the summation code to write up.
    Last edited by Adak; 01-12-2008 at 08:16 AM.

  4. #49
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i'm done with the summation code.. anyway if possible can u copy my code and give explain it using the comment line on the program itself.. i wil be able to understand better(=

    Code:
    #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int arrayofprimes[100];// create space for array
         int top,remainder,numberofprimes,prime,i;  
         int minusONE;//pointer for array
    
    
     
       
       printf("pls enter top limit:");
       scanf("&#37;d",&top);
    
         prime = 1; 
         numberofprimes = 0;
    
    
       do
       {
          if(top > 5) 
              {
              minusONE = top / 2;
              }
          else
             { 
               minusONE = top - 1;
             }
    
    
           for(minusONE; minusONE > 1; minusONE--) 
     
                {
                remainder = top % minusONE;
                     if(remainder==0)  //it's not a prime number, so stop processing it.
                    {
                        prime = 0;
                        break;  //break out of the for loop.
                    }
            } //end of for loop
                   
            if(prime==1) 
                {
                    arrayofprimes[numberofprimes++] = top;
                }
                
                                       
             prime=1;
             }while(top-- >1);
         
           
          for(i = 0; i < numberofprimes; i++)
         {
              printf("\nthe prime number in array[%02d]is %d ",i,arrayofprimes[i]);
         }
          printf("\n");
    
    
         system("pause");
         return 0;
       
         
     }

  5. #50
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    That's not going to happen sorry. It's your code and your responsibility to know what it does. If you don't know how it works at the point you submit it, then ultimately that amounts to copying others work.
    If you want the extra marks for commenting it, then you should spend the time to understand what you have, and comment it yourself.

    If you really did already understand the maths and logic of how the program should work, and just had issues with the C syntax, then it should be pretty darn easy to figure out.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #51
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, if it wasn't NFL playoff weekend . . . nah, not really.

    You need to step through the code and watch the variables as they change. Try to predict what they'll be on the next loop. Buddy on up to that code, a bit.

    How would you describe the line of code? The obvious lines don't need a comment, But minusONE needs to be described better than "pointer to an array"! Both i and minusONE are loop control variables.

    Do you need to include conio.h? What about stdlib.h? What's in these header files that your program uses?

    And one last thing:

    A statement in C always ends with a semi-colon. No exceptions.
    Code:
    if(top < 5) 
       top = top/2;
    Needs no curly braces.

    Code:
    for(i = 0; i < some_number; i++)
       array[i] = (1/2 base * height);      //work with triangles now! :)
    And the for statement ends with the semi-colon also, and needs NO curly braces. Both the above are just ONE single statement.

    ONLY when you want to have more than one statement as part of another statement (logically), do you need the curly braces.
    Code:
    if(top < 5)
    {
         top = top/2;               //first statement
         area = 1/2 base * height;  //second statement
    }
    Here, the if statement needs the curly braces, because it encloses two or more statements, together.

    Pimp, I've gone beyond the normal boundaries of being helpful, because I thought you really needed the extra help. It's way too time consuming to this again, however.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help, trying to find prime number
    By vampirekid.13 in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2009, 03:29 PM
  2. Prime number algorithm
    By Akkernight in forum C Programming
    Replies: 9
    Last Post: 04-19-2009, 01:50 PM
  3. how to calculate billion number
    By mrmamon in forum C++ Programming
    Replies: 9
    Last Post: 10-16-2005, 06:34 AM
  4. prime number finder.
    By Aalmaron in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2004, 03:56 PM
  5. Replies: 3
    Last Post: 01-14-2003, 09:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21