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; I believe both Manav and myself determined Pimp is not a good candidate to write a "Sieve" program. My advice ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe both Manav and myself determined Pimp is not a good candidate to write a "Sieve" program.

    My advice to Pimp was to use the simpler nested loops, and the mod operator for testing.

    He needs to post up what he's trying now, so we can see what he's doing.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Which is the problem, do you lack the math and logic skills to understand how to algorithmically find a prime number, or are you just unsure how to program in C?
    If it is the later, please provide full pseudocode for how you would do it, and we'll tell you what the correct syntax is to get from there to a working program.
    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"

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i know the way to find prime number but i can't get it into C programming..

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pimp, show us what you're trying to make work.

    It may help to just do one of the two nested loops, at a time. That is, for now just mark out the outer loop, and work on the inner loop, only.

    /* while(blah blah blah) this is the outer loop, now marked out so the C compiler will ignore it */

    for( blah blah; blah < blah; blah) /* this is the inner loop, and you can concentrate on it only */

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Code:
    #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int numarray[100];// create space for array
         int top,reminder,flag,value,prime;
         int minusONE;//pointer for array
     
       do
         {
             printf("pls enter top limit:");
             scanf("%d",&top);
            for(minusONE = top - 1; minusONE > 0; minusONE--)
            {
                reminder = top % minusONE;
                printf("\n%d / %d reminder is %d", top, minusONE, reminder);
               /* if(reminder==0)
                {
                   top!=prime;
                                
                }
                if(reminder!=0)
                {
                  top==prime;                            
                }
            for(minusONE=top-1;minusONE;minusONE--)
            {
               printf("numarray[%02d]=%d\nn",top,numarray[top]);                     
            }
                _sleep(1000);
            
            }*/
            
         }while(minusONE >=3&&flag !=0);
         {
                         minusONE--;
                         flag=value%minusONE;
         }
         system("pause");
         return 0;
       
         
     }
    this is the code that i did.. i only manage to get the remainder..
    after which i have to store thse that rem!=0 into my array as prime number
    then i have to print out those number and sum up.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK. You have two nested loops, that's good. It's awkward working from the top, downward. For example, if the top number is 357, now we need to see how the code works.

    So say we start working downward from 356, as your code does: 355 - not prime, 354 - not prime, 353 - not prime.

    Eventually, it's going to hit you that you're doing a ........load of looping for nothing up here. You'll find *NO* primes for a number, above the square root of the number, see?

    And do you know just off-hand if 357 can be divided evenly by say, 91? So it's horribly tedious to check your code, when you work from the top down.

    Also, when remainder = 0, that number is NOT prime. Primes are numbers that have nothing that will divide into them evenly, except 1, of course.

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    ya know the part that if the rem is not 0 it will not be a prime but i can't get that part and save it into a array.. that the part i'm stuck and lost

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    ya. i know that part*

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by pimp View Post
    Code:
    #include<conio.h>
    #include<stdlib.h>
    #include<stdio.h> 
     
     
     int main()
     {
         int numarray[100];// create space for array
         int i, top, remainder, numberOfPrimes, prime;  Remainder, not reminder
         int minusONE;//pointer for array
    
    
     
    //Has to be moved out of the two nested loops for finding primes.
       printf("pls enter top limit:");
       scanf("&#37;d",&top);
    
       prime = 1; 
       numberOfPrimes = 0;
    
    
       do
       {
           for(minusONE = (top / 2); minusONE > 1; minusONE--) //Top / 2 is not the best, sqrt top is, but it's way better than top - 1
           {
                remainder = top % minusONE;
                printf("\n%d / %d remainder is %d", top, minusONE, remainder);
                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 
            // if prime == 1 at this point, you have a prime number. Add this if statement, and put the prime number, into the array.
           
            if(blah blah blah) 
            {
                array of primes[numberOfPrimes++] = top;
                
                                       
            prime = 1;
         }while(--top > 1);
         
       //print up the arrray of primes
       for(i = 0; i < numberOfPrimes; i++)
          print array of primes[i];
    
    
    
    
         system("pause");
         return 0;
       
         
     }
    this is the code that i did.. i only manage to get the remainder..
    after which i have to store thse that rem!=0 into my array as prime number
    then i have to print out those number and sum up.
    I've made some changes, some highlighted, others are not, but easy to see.

    Edit: OK, you want to save the primes into an array and print them out later.
    I removed two loops near the bottom - no idea what they were about.
    Last edited by Adak; 01-12-2008 at 06:17 AM.

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i saw some aid in there.. but i have a question. how do i stop processing if the remainder is =0.
    i have the planning there but i some how can't get it into the program

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    you break out of the for loop (the inner loop, with a break statement, after setting prime = 0;

  12. #27
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    i don understand. can u type the code out ?basically i'm stuck at converting into C from my logic

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope.

    Look at post #24, it shows a lot of the logic you need, tucked into your program.

    All untested, of course, but it's "close", and better than pseudo-code, which would do you little good.

  14. #29
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Consider writing it as two functions

    eg, in main() you have your prompts and this
    Code:
    for ( p = 1 ; p < numPrimes ; p++ ) {
      if ( isPrime(p) ) {
        printf("&#37;d is prime\n", p );
      }
    }
    Then a function like this
    Code:
    int isPrime ( int p ) {
      int result = 0;
      if ( p % 2 == 1 ) {
        result = 1;
      }
      return result;
    }
    Obviously it isn't correct, but it's enough to test that main() is actually prompting and looping correctly for example. When it is doing what you want, you're free to work on the detail of working out whether a given number is prime or not.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    there is a part that i don't understand can explain more?

    }while(--top > 3);

    //print up the arrray of primes
    for(i = 0; i < numberOfPrimes; i++)
    print array of primes[i];


    system("pause");
    return 0;


    }

Page 2 of 4 FirstFirst 1234 LastLast
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