Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Prime Number Algorithm

Name: Anonymous 2008-12-08 21:47

/prog/ I'm very deeply stressed at the moment. I have an assignment due tomorrow morning, a prime number algorithm. I know I screwed it up badly, and I'm not even sure if I am on the right track. If there are any capable c++ programmers on /prog/ right now, think you could lend a gal a hand?

Here is a copy of my current code:

#include <iostream>
using namespace std;

int main()
{
    int s;
    int p;
    long PrimeStatus[100001];
    PrimeStatus[s] = 1;
    long MAX_PRIME_CHECK = 100000;

    for(int s = 1; s < MAX_PRIME_CHECK; s++)
    {
        if(p > 2)
        {
            PrimeStatus[p] = 1;
            return 0;
        }
       
        else if(p <= 2)
        {
            if(PrimeStatus[p] = 1)
            {
                PrimeStatus[p] = 1;
                return 0;
            }
           
            else if(PrimeStatus[p] = 0)
            {
                if(p%(p-1) == 0)
                {
                    PrimeStatus[p] = 0;
                    return 0;
                }
               
                else while(p%(p-1) != 0)
                {
                    p = p-1;
                }
               
                if(p%(p-1) == 0)
                {
                    PrimeStatus[p] = 0;
                    return 0;
                }
            }
        }
    for(p = 1; s = 0; p++)
    {
        cout << PrimeStatus[p] << endl;
    }
    return 0;
    }
}

Name: Anonymous 2008-12-08 22:24

>>7

Thank you. I did notice that. Here is a slightly updated version of the code. While it still does not produce the desired outfit, it is a step up from what it was before hand. I am not exactly sure if I understand exactly where you are coming from with the not initializing PrimeStatus.


#include <iostream>
using namespace std;

int main()
{
    long PrimeStatus[100001];
    long MAX_PRIME_CHECK = 100000;

    for(int s = 1; s < MAX_PRIME_CHECK; s++)
    {
        if(s > 2)
        {
            PrimeStatus[s] == 0;
            return 0;
        }
       
        else if(s <= 2)
        {
            if(PrimeStatus[s] == 1)
            {
                PrimeStatus[s] == 1;
            }
           
            else if(PrimeStatus[s] == 0)
            {
                if(s%(s-1) == 0)
                {
                    PrimeStatus[s] == 1;
                }
               
                else while(s%(s-1) != 0)
                {
                    s = s-1;
                }
               
                if(s%(s-1) == 0)
                {
                    PrimeStatus[s] == 1;
                }
            }
        }
       
        if(PrimeStatus[s] == 0)
        {
            cout << s << endl;
        }
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List