Question:
A C++ Pogramming Problem (Displaying Prime Numbers)?
?
2009-12-22 06:50:05 UTC
I was trying to program a program that displays Prime Numbers starting from 2 until the number the user specifies "n"("n" is the number of prime numbers to display and not the value of the last prime number to display) , but it didn't work so could you help me and tell me what is the problem and where is it.
Here is the programming code

#include
using namespace std;
int main ( )
{
float i, x;
int step, n, count=0;
int keep=3;
cout<<"Enter the number of prime numbers you want"< cin>>n;
cout<<"2";
for (step=1; step<=n; step++; i++)
for (i>=3)
{
keep = count;
for (x=2; x<=i-1; x++)
{
if (i%x != 0)
count = i;
else if (i%x == 0)
{
count = keep;
break;
}
}
cout<<", "< break;
}
return 0;
}

The program was suppose to tell the user "Enter the number of prime numbers you want", and when the user enters a number "n" the program is suppose to display the numbers this way

2, 3, 5, 7, 11, ......................., the Nth Prime Number
Three answers:
cja
2009-12-22 12:13:11 UTC
Another responder mentioned the sieve of Eratosthenes, and that's what I like to use for problems such as this. See below for a way to do it. It's a bit extravagent in its use of memory, but once the sieve is initialized it's very quick and easy to pick out the prmes.



#include

#include

#include

#include

#include



using namespace std;



size_t drainSieve(deque &);

size_t N = USHRT_MAX * 2048;



int main(int argc, char *argv[]) {

    unsigned x, i, n, numPrimes;

    string in;

    stringstream ss;



    // Find primes

    deque sieve(N,true);

    numPrimes = drainSieve(sieve);



    // Prompt user to query for primes

    while (true) {

        cout << endl << "Enter number of primes to find (0 .. ";

        cout << numPrimes << ")" << endl;

        cout << "> ";

        getline(cin,in);

        ss.clear(); ss.str(in);

        if ((ss >> x) && (x <= numPrimes) && !ss.good()) {

            i = n = 0;

            while ((n < x) && (i < sieve.size())) {

                if (sieve[i] == true) {

                    cout << setw(8) << i << " ";

                    n++;

                    if ((n % 8) == 0) cout << endl;

                }

                ++i;

            }

            cout << endl;

        }

    }

    return 0;

}



size_t drainSieve(deque &ps) {

    size_t N = ps.size(), count = 0;



    ps[0] = ps[1] = false;

    for (size_t i = 2; i*i < N; i++) {

        if (ps[i] == true) {

            for (size_t j = i+i; j < N; j += i) {

                ps[j] = false;

            }

        }

    }

    for (size_t i = 0; i < N; i++) {

        if (ps[i] == true) count++;

    }

    return count;

}



#if 0



Sample run:



Enter number of primes to find (0 .. 7603438)

> 32

              2 3 5 7 11 13 17 19

            23 29 31 37 41 43 47 53

            59 61 67 71 73 79 83 89

            97 101 103 107 109 113 127 131



#endif
?
2009-12-22 07:31:58 UTC
i won't fix your current program's many mistakes, but i will try pointing out some obvious ones i can see:

#include

using namespace std;

int main ( )

{

float i, x;

int step, n, count=0;

int keep=3;

cout<<"Enter the number of prime numbers you want"<
cin>>n;

cout<<"2";

for (step=1; step<=n; step++; i++) //for takes max 3 operands

for (i>=3) //if you use only one operand you must use it like this ( ; ; i>=); you are using i without initializing it (it doesnt have a value)

{

keep = count;

for (x=2; x<=i-1; x++)

{

if (i%x != 0) // you cant use % with float

count = i;

else if (i%x == 0)

{

count = keep;

break;

}

}

cout<<", "<
break;

}

return 0;

}

far from being all the mistakes but hope you will learn something from here. (doesn't your compiler really point out the mistakes?) you could check this link to get an easily usable algorithm: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
CraZee
2009-12-22 06:56:25 UTC
try including " #include "


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...