Pseudocode for prime numbers

Pages: 123
This is the loop I made.

1
2
3
4
5
6

for(i=2;i<num;num++)
if(num%i==0)
return false;
else
return true;
Last edited on
1
2
3
4
5
for(i=2;i<num;num++)
{
    if(num%i==0) return false;
}
return true;
So will you use my algorithm or you find that yours is easier to understand?
Yours looks lovely, but I'd prefer to use mine just because I need to understand as well haha.

Is my loop correct, however?

As for your programmer, yes, that would be nice.

I hold no grudges. I just responded corresponding to how I felt.

Sorry for the non-related ruckus.
Last edited on
> Yours looks lovely, but I'd prefer to use mine just because I need to understand as well haha.
I'll wait until you are ready :)

> Is my loop correct, however?
Your loop wasn't correct, that's why I fixed it for you just now. If you are still in doubt, just update and show us your loop.
Oh! You fixed mine? I couldn't even tell. Is it the else statement that made is incorrect?

1
2
3
4
5
for(i=2;i<num;num++)
{
    if(num%i==0) return false;
}
return true;


This one is the correction, correct?
Last edited on
Yes :)
Alright, here is my corrected code. I think I got the bool function fixed.

However, I'm still getting a weird output for some reason.

http://prntscr.com/boq669
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <string>
using namespace std;
//module prototype
bool isPrime (int number);

const int ZERO_FOR_CAL=0;

int main()
{
    int number;
    string keepGoing;
    bool trueOrFalse;

    cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;

    do
    {
        cout << "Enter a positive number." << endl;
        cin >> number;
        trueOrFalse=isPrime(number);

        if (trueOrFalse==true);
        {
            cout << "The number is prime." << endl;
        }
        if (trueOrFalse==false);
        {
            cout << "The number is not prime." << endl;
        }
        cout << "Would you like to see if another number is prime? Type yes if so." << endl;
    }
    while (keepGoing=="yes");
}



bool isPrime(int number)
{


    int i=2;

    for(i=2; i<number; number++)
    {
        if(number%i==ZERO_FOR_CAL) return false;
    }
    return true;




}


Redundant colons( ; ) after the if condition parts.
1
2
3
4
5
6
7
8
9
if (trueOrFalse==true);
        {
            // . . .
        }

        if (trueOrFalse==false);
        {
            // . . .
        }
http://prntscr.com/boqc6x

I removed them, but then it gave me these two errors.

(I assume you meant remove.)
Sorry, but you should also post your code that consists of the errors here, too. :)
I hope you meant the whole thing, haha.

It says I need a semicolon at the end of each conditional in my bool function.

As for the logic errors when I run it, I'm not sure why they're appearing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
using namespace std;
//module prototype
bool isPrime (int number);

const int ZERO_FOR_CAL=0;

int main()
{
    int number;
    string keepGoing;
    bool trueOrFalse;

    cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;

    do
    {
        cout << "Enter a positive number." << endl;
        cin >> number;
        trueOrFalse=isPrime(number);

        if (trueOrFalse==true);
        {
            cout << "The number is prime." << endl;
        }
        if (trueOrFalse==false);
        {
            cout << "The number is not prime." << endl;
        }
        cout << "Would you like to see if another number is prime? Type yes if so." << endl;
    }
    while (keepGoing=="yes");
}



bool isPrime(int number)
{


    int i=2;

    for(i=2; i<number; number++)
    {
        if(number%i==ZERO_FOR_CAL) return false;
    }
    return true;




}


line 23 and 27, look closely.... see something unusual?
Three logic errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

using namespace std;

// module prototype
bool isPrime (int number);
const int ZERO_FOR_CAL=0;
int main()
{
    int number;
    string keepGoing;
    bool trueOrFalse;
    cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;
    do
    {
        cout << "Enter a positive number." << endl;
        cin >> number;
        trueOrFalse=isPrime(number);
        if (trueOrFalse==true) // (1)
        {
            cout << "The number is prime." << endl;
        }
        if (trueOrFalse==false) // (2)
        {
            cout << "The number is not prime." << endl;
        }
        cout << "Would you like to see if another number is prime? Type yes if so." << endl;
    }
    while (keepGoing=="yes");
}

bool isPrime(int number)
{
    int i=2;
    for(i=2; i<number; i++) // (3)
    {
        if(number%i==ZERO_FOR_CAL) return false;
    }
    return true;
}
Found it!

Fixed most of the logic errors, too!

Alright, the two isses, 1) I wanted the user to press yes at the end if they wanted to enter another prime number, but when I run it and try to press yes, it exits me out of the program due to the (press any key to exit).

If what I just said confused you, perhaps run it yourself and you'll see what I mean.

I wanted to make it so that if they press yet they repeat the process.

2) It's giving me weird answers for the prime is not prime.

For example when I put 3 or 5 it says it's not prime when they are.

Only 1 and themselves are factors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
//module prototype
bool isPrime (int number);

const int ZERO_FOR_CAL=0;

int main()
{
    int number;
    string keepGoing;
    bool trueOrFalse;

    cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;

    do
    {
        cout << "Enter a positive number." << endl;
        cin >> number;
        trueOrFalse=isPrime(number);

        if (trueOrFalse==true)
        {
            cout << "The number is prime." << endl;
        }
        if (trueOrFalse==false)
        {
            cout << "The number is not prime." << endl;
        }
        cout << "Would you like to see if another number is prime? Type yes if so." << endl;
    }
    while (keepGoing=="yes");
    system("Pause");
}



bool isPrime(int number)
{


    int i=2;

    for(i=2; i<number; number++)
    {
        if(number%i==ZERO_FOR_CAL) return false;
    }
    return true;




}
Last edited on
You just didn't notice the third logic error.
for(i=2; i<number; number++ i++) // (3)

Declare an additional string and tell cin to input it :
1
2
3
4
5
6
7
8
9
do
{
    char YesNo[100];
    cout << "Would you like to see if another number is prime? Type yes if so." << endl;

    cin >> YesNo;
    keepGoing=YesNo;
}
    while (keepGoing=="yes");
We fixed everything! It runs perfectly.

Thanks so much for the patience!
Glad to hear :)
After checking indentation and comments this is the result.

Just leaving here for future reference. : )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Ashton Dreiling 
//Prime number exercise 
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
//module prototype
bool isPrime (int number);

//global constant
const int ZERO_FOR_CAL=0;

int main()
{
    //some variables
    int number;
    bool trueOrFalse;
    string keepGoing;

    cout << "We are going to see if the positive integer you enter is a prime number or not." << endl;

    do
    {
        cout << "Enter a positive number." << endl;
        cin >> number;
        trueOrFalse=isPrime(number);
        
        

        //testing trueOrFalse value
        if (trueOrFalse==true)
        {
            cout << "The number is prime." << endl;
        }//end if statement
        if (trueOrFalse==false)
        {
            cout << "The number is not prime." << endl;
        }//end if statement


        cout << "Would you like to see if another number is prime? Type yes if so." << endl;
        cin >> keepGoing;
    }//ed while loop
        while (keepGoing=="yes");
        system("Pause");
}//end main 


//bool function to put in prime loop 
bool isPrime(int number)
{


    int i=2;
    
    //for loop for prime calculation 
    for(i=2; i<number; i++)
    {
        if(number%i==ZERO_FOR_CAL) return false;
    }//end for loop
    return true;




}//end bool function  
finally!!! :)
Topic archived. No new replies allowed.
Pages: 123