Outputting Prime Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flyaway888
    New Member
    • Mar 2007
    • 35

    Outputting Prime Numbers

    Hey All,
    I need a program to input a number, test if it is a prime number or not and then output it if it is prime or output the next prime number if it is not.
    So far I have to input section, the function and the prime number test so it works if it is prime but I can't figure out how to output the next prime number.
    Any help would be much appreciated.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Hello,
    for the next prime number can't you just loop the prime number testing function from the non-prime until it finds a prime number and then return that. If that doesn't work, how does your function work then?

    Comment

    • flyaway888
      New Member
      • Mar 2007
      • 35

      #3
      I wasn't sure how to loop a function. Do I just put it in a for loop like any other value?

      Comment

      • seninfothil
        New Member
        • Apr 2007
        • 3

        #4
        hi friend
        i red you question .
        let a be the number is not a prime ...
        then we go for a looping statement form a to a+100...
        i am sure that there must be a prime in between that range....

        Comment

        • palani12kumar
          New Member
          • Oct 2006
          • 60

          #5
          Originally posted by seninfothil
          hi friend
          i red you question .
          let a be the number is not a prime ...
          then we go for a looping statement form a to a+100...
          i am sure that there must be a prime in between that range....

          I think you need not to go on checking upto next 100 numbers to find the next prime of the number which is already been found not to be a prime.
          its enough to try something like this:

          Code:
           read the value of a;
          do{
           if a is not a prime
           i)increment a by 1(ie.)a++ );
           ii)check whether a++ is prime or not
              a.)if a++ is prime, print the result and get out of the loop
              b.)else continue the process
          }while(a is not a prime)

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by seninfothil
            hi friend
            i red you question .
            let a be the number is not a prime ...
            then we go for a looping statement form a to a+100...
            i am sure that there must be a prime in between that range....
            *ahem* The following two numbers are both prime numbers and are more than
            100 apart (they are the smallest two primes having this property)

            370261 370373

            kind regards,

            Jos

            Comment

            • flyaway888
              New Member
              • Mar 2007
              • 35

              #7
              Thanks for all your help,
              I have tried doing the do...while loop and the output comes up with nothing and I can't understand why.
              I use an if..else statement to check if it is prime at first and output it if it is. This all works. Then from the else part I did this loop...

              Code:
              else
                     do
                     n++;
                     while (n != prime);
                     
                     cout << n << endl;
              Any ideas why this wouldn't work?

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                You have forgotten the brackets around your loop. You need

                Code:
                do {
                   n++;
                } while (n != prime);
                Also, shouldn't you be using your prime() function? This looks like you are comparing n with another number.

                Comment

                • flyaway888
                  New Member
                  • Mar 2007
                  • 35

                  #9
                  Oh I did have brackets at one point but I keep changing it! I change to function() to prime

                  Code:
                  bool prime;
                     prime = isprime (n);
                     if (prime)
                     cout << n << endl;
                     do
                     { 
                          n++;
                     } while (!prime);  
                     cout << n << endl;
                  So its actually like this
                  But with brackets it still doesn't work and I don't understand why.

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    You should probably use a while loop rather than a do...while loop, and you should have the loop under an else clause instead of after your if statement.

                    Comment

                    • flyaway888
                      New Member
                      • Mar 2007
                      • 35

                      #11
                      So you mean something like this...

                      Code:
                      bool prime;
                         prime = isprime (n);
                         if (prime)
                         cout << n << endl;
                         else
                         while (!prime)
                         {
                               n++;
                         }  
                         cout << n << endl;
                      Because this does the same thing, outputs nothing.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by flyaway888
                        So you mean something like this...

                        Code:
                        bool prime;
                           prime = isprime (n);
                           if (prime)
                           cout << n << endl;
                           else
                           while (!prime)
                           {
                                 n++;
                           }  
                           cout << n << endl;
                        Because this does the same thing, outputs nothing.
                        If variable 'prime' was false to start with it stays false in your code because
                        you never attempt to change it anywhere in your loop.

                        I'd simply do this:
                        Code:
                        for (; !isPrime(n); n++)
                           ;
                        cout << n << endl;
                        kind regards,

                        Jos

                        Comment

                        Working...