Pseudocode for prime numbers

Pages: 123
Jul 4, 2016 at 2:24am
This is just my pseudocode, but I'm having trouble with one portion.


//module prototype
Boolean isPrime(int number)

main ()
[int number

display we are going to see if the positive integer you enter is a prime number or not
do
[
display enter a positive number

input number

trueOrFalse=isPrime(number)

if (trueOrFalse == true)
[the number is prime]
else
[the number is not prime]
]

bool isPrime(int number)
[
bool status

if (number%2==0)
[status=true]
if(number%3==0_
[status=true]
else
[status==false]
return status
]



if (number%2==0)
[status=false]
if(number%3==0_
[status=false]

This is the portion I'm having trouble with.

While I'm pretty okay with how to set my program up, I'm not too sure how to calculate if a number is prime or not int the actual code although I do know what a prime number is.

I figured if it gives a remainder or 0 if it's divided by 2 or 3 the number would not be prime, but I don't believe that'll work.

What should I do to fix this?

Last edited on Jul 4, 2016 at 2:27am
Jul 4, 2016 at 2:37am
Hi,

Instead of dividing it by 2 and 3 divide it by every number which is half of the number

eg.

if your number is 117 the get a loop which checks for remainder from 2 to 117/2
Jul 4, 2016 at 3:19am
I'm a bit confused what you mean.
Jul 4, 2016 at 3:48am
pseudo code:

1
2
3
4
5
6
7
8
9
10
11
12
get number

while(the counter is not equal to number/2)
{
check for prime
if prime is true
get out of loop and print its prime
else 
increment the counter
}
if the prime is not true 
print the number is not prime


HOPE IT HELPS
Last edited on Jul 4, 2016 at 3:48am
Jul 4, 2016 at 3:55am
I'm still confused because you're not using my pseudocode. I'm sorry. I'm not doing very good tonight. Can you use my pseudocode to help me?
Jul 4, 2016 at 3:59am
replace my pseudo-code in your bool isprime function
Jul 4, 2016 at 4:30am
#include <iostream>
#include <string>
using namespace std;
//module prototype
bool isPrime (int number);

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)
{
counter = 2

while(counter<number)
{
if (number%counter==0)
{
return false
}
else
{
return true
}
}
}

return 0;
}

Alright, just finished my code. Tell me if I made any mistakes.

I currently have two errors I'm not sure how to fix. Can you tell me how to fix them?

Also, did I do anything logically wrong?
http://prntscr.com/bohp5t
Last edited on Jul 4, 2016 at 5:17am
Jul 4, 2016 at 4:41am
Please use code tags
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
#include <iostream>
#include <string>
using namespace std;
//module prototype
bool isPrime (int number);

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");
//there should be a bracket here
bool isPrime(int number)
{
counter = 2//initialize it as int
//---------------------------------this whole section is logically flawed----------------
while(counter<number)
{
if (number%counter==0)
{
return false//semicolon?
}
else
{
return true//semicolon
}
}
//---------------------------------this whole section is logically flawed----------------
}

return 0;//returning 0 is not a good idea
}
Jul 4, 2016 at 4:42am
Alright, so how do I fix the logic?
I'm still getting the same errors (when I added the bracket you told me to add it gave me another error)

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
http://prntscr.com/boht1w
This is the code I have now (it indented for me so tell me if my indents are wrong) 
#include <iostream>
#include <string>
using namespace std;
//module prototype
bool isPrime (int number);

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 counter = 2

                  while(counter<number)
        {
            if (number%counter==0)

                return false;

                       else

                           return true;

                }
    }

    return 0;
}
Last edited on Jul 4, 2016 at 5:18am
Jul 4, 2016 at 5:00am
Jul 4, 2016 at 5:05am
I'm sorry, but I don't understand that.

Would you mind pointing it out in my code?
Jul 4, 2016 at 5:06am
closed account (48T7M4Gy)
Hey FB, you're asking about pseudocode and demand people correct it for you.

Your first step is to actually write pseudocode. What you have so far presented is nothing like it. I suggest you go back and revise your notes/text, see a tutor/prof. At present unfortunately you are just spinning your wheels :)

As a tip visit Wikipedia on primes. There is ample material there on pseudocode and primes, individually and combined. Come back by all means when you have something we can all call pseudocode . :)
Jul 4, 2016 at 5:11am
Actually, we're fixing my real code now. Didn't you see? Haha. :)

Anyways, I fixed the errors, but it's giving me this output. How do I fix it? It keeps saying the number is and isn't prime and when I try to type yes to enter another number it won't let me.
http://prntscr.com/bohzf6

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
#include <iostream>
#include <string>
using namespace std;
//module prototype
bool isPrime (int number);

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 counter = 2;

    while(counter<number)
    {

        if (number%counter==0)

            return false;

        else

            return true;
    }





    return 0;
}
Last edited on Jul 4, 2016 at 5:18am
Jul 4, 2016 at 5:12am
closed account (48T7M4Gy)
Use code tags
Jul 4, 2016 at 5:14am
What do you mean?
Jul 4, 2016 at 5:14am
closed account (E0p9LyTq)
How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/
Jul 4, 2016 at 5:16am
firstly you are not incrementing counter, and like I said before your isprime function is faulty, I have given you pseudo-code beofre, try to understand it and edit your code
Jul 4, 2016 at 5:17am
I have. I don't understand it.

Also, thanks FurryGuy. Will do!
Jul 4, 2016 at 5:18am
closed account (48T7M4Gy)
What do I mean? You're joking right? Use the same tag system you've used on all your other numerous threads where you expect people to do your homework for you.

Jul 4, 2016 at 5:19am
Why don't you point out one time I asked someone to do my homework for me since I
apparently do this?

I'm awfully curious.

If I was kidding, I wouldn't have asked.

Sometimes I don't understand words normally.

Last edited on Jul 4, 2016 at 5:21am
Pages: 123