Prime No. generator

Discussion in 'C++' started by ume$h, Apr 21, 2007.

  1. ume$h

    ume$h Guest

    Why does this program fail to run?

    //FIRST n PRIMES
    #include <iostream.h>
    #include <math.h>

    int main(void)
    {
    long int i,j=3,count=1;
    int n,flag=0;
    cout<<"How many primes?";
    cin>>n;
    cout<<"2, ";
    while(count<n)
    {
    for(i=1;i<(sqrt(j)+1);++i){
    if(j%i==0) {flag=1;break;}
    if(j>3 && flag==0) {cout<<j<<", ";++count;}}
    ++j;
    }
    return 0;
    }
    ume$h, Apr 21, 2007
    #1
    1. Advertising

  2. ume$h

    Heinz Ozwirk Guest

    "ume$h" <> schrieb im Newsbeitrag
    news:...
    > Why does this program fail to run?
    >
    > //FIRST n PRIMES
    > #include <iostream.h>
    > #include <math.h>
    >
    > int main(void)
    > {
    > long int i,j=3,count=1;
    > int n,flag=0;
    > cout<<"How many primes?";
    > cin>>n;
    > cout<<"2, ";
    > while(count<n)
    > {
    > for(i=1;i<(sqrt(j)+1);++i){
    > if(j%i==0) {flag=1;break;}
    > if(j>3 && flag==0) {cout<<j<<", ";++count;}}
    > ++j;
    > }
    > return 0;
    > }


    1) How is a prime defined? Especially, what does the definition say about
    dividing a prime by 1?
    2) Is 3 a prime?
    3) Are there any even primes except 2.
    4) What happens to 'flag' once your program found a prime?
    5) How often does your program compute sqrt(j) for each j?

    Perhaps your answers to these questions will help you solving your problem.

    HTH
    Heinz
    Heinz Ozwirk, Apr 21, 2007
    #2
    1. Advertising

  3. ume$h

    osmium Guest

    "ume$h" writes:

    > Why does this program fail to run?
    >
    > //FIRST n PRIMES
    > #include <iostream.h>
    > #include <math.h>
    >
    > int main(void)
    > {
    > long int i,j=3,count=1;
    > int n,flag=0;
    > cout<<"How many primes?";
    > cin>>n;
    > cout<<"2, ";
    > while(count<n)
    > {
    > for(i=1;i<(sqrt(j)+1);++i){
    > if(j%i==0) {flag=1;break;}


    Where is the code that sets flag equal to 1?
    Can you get out of the loop without setting the flag?

    You seem to be using Windows so here's a tip:

    Activate the task mgr. Then activate your program. If the green icon in
    the task bar turns bright green, you program is in a tight loop. (As here.)

    > if(j>3 && flag==0) {cout<<j<<", ";++count;}}
    > ++j;
    > }
    > return 0;
    > }
    >
    osmium, Apr 21, 2007
    #3
  4. ume$h

    ume$h Guest

    //This one runs(Ultimately by self attempt!)

    #include <iostream.h>
    #include <math.h>
    int main(void)
    {
    long int i,j=3,count=1;
    int n;
    cout<<"\nHow many first primes do you want to generate? ";
    cin>>n;
    if(n<0) {cout<<"Wrong No.\n Enter No. Again"; cin>>n;}
    if(n==1 || n>1)cout<<"2, ";
    if(n>1) while(count<n)
    {

    int flag=0;
    for(i=2;i<sqrt(j)+1;++i)

    if(j%i==0)
    {
    flag=1;break;
    }

    if(flag==0)
    {
    ++count;cout<<j<<", ";
    }
    ++j;

    }

    return 0;
    }
    ume$h, Apr 21, 2007
    #4
  5. ume$h

    red floyd Guest

    ume$h wrote:
    > Why does this program fail to run?

    Define "fail to run". Does it not compile? Does it give erroneous output?
    See FAQ 5.8 http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8


    > //FIRST n PRIMES
    > #include <iostream.h>

    Non-standard header. #include <iostream> isntead

    > #include <math.h>

    using namespace std; // to get cin and cout.

    >
    > int main(void)

    void in this context is superfluous and discouraged in C++.
    > {
    > long int i,j=3,count=1;
    > int n,flag=0;

    poor practice. Better practice is one declaration per line.
    Define i inside the for loop. e.g. for (int i = 1; ...)
    > cout<<"How many primes?";
    > cin>>n;
    > cout<<"2, ";
    > while(count<n)
    > {
    > for(i=1;i<(sqrt(j)+1);++i){

    Hint: if you only check odd numbers for primality (see below), you can
    increment i by 2 instead of 1.

    > if(j%i==0) {flag=1;break;}

    Your problem lies here. Hint. What is the initial value of i?
    What is the value of j%i, for any j, given that initial value of i?

    Also, formatting -- put the body of the if on a separate line.
    > if(j>3 && flag==0) {cout<<j<<", ";++count;}}

    Ditto here.

    > ++j;

    Hint: 2 is the only even prime. You can bump j by 2.
    > }
    > return 0;
    > }
    >
    red floyd, Apr 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tuvas
    Replies:
    20
    Views:
    957
    Tuvas
    Mar 6, 2006
  2. Joel Mayes

    Beginners prime number generator

    Joel Mayes, Dec 5, 2006, in forum: C Programming
    Replies:
    10
    Views:
    525
    Joel Mayes
    Dec 13, 2006
  3. John Posner

    Re: Infinite prime number generator

    John Posner, Jun 29, 2010, in forum: Python
    Replies:
    0
    Views:
    428
    John Posner
    Jun 29, 2010
  4. Jeremy Fischer
    Replies:
    0
    Views:
    184
    Jeremy Fischer
    Jan 16, 2005
  5. Replies:
    7
    Views:
    166
    -berlin.de
    Mar 23, 2007
Loading...

Share This Page