Pseudocode for prime numbers

Pages: 123
closed account (48T7M4Gy)
Generalised questions like "how do I fix it" for one are a more than subtle giveaway. There are dozens of other examples that breach the spirit of this forum. By all means bring your prof onto the forum and I am happy to explain it to him so that he can see the need for your remedial classes.
Using my title names that are highly subjective as proof I ask people to do my homework isn't evidence.

I've never once asked anyone to directly do my homework for me, so I'd appreciate if you'd stop spreading misinformation because my title names are subjective and you like to jump to conclusions.

My professor knows I use this site. He allows code to be confirmed before it's submitted, hints to code corrections, and allows a few direct code corrections if one has been spending awhile trying to figure it out. He just doesn't want his students to copy work from others and be given all the answers to every piece of the code.

I'm sure you wouldn't know that I don't ask for people to do my homework or ask for help with my whole code anyways since you decided to make a judgement off of my material based off of my subjective title names.

Now, are you going to help me, or as you'd put it, do my work for me?

Or should we start arguing about my suggestive titles names?

Last edited on
closed account (48T7M4Gy)
If only you spent more time on learning and applying C++ instead of pleading such a weak denial case.
Which, until you can link one of my threads where I actually asked someone to do my homework, you're just spewing words out of your mouth that seem convenient because of my suggestive titles names.

I have sensory processing issues. It's hard for me to process words, and my professor and tutors for my university aren't always there. Yeah, I need a lot of help. I get it, you're annoyed with my threads.

The great thing about this, is you don't have to help me if you're so disappointed with the way I format the names of my titles.

I'm in many other classes, a couple summer programs, and I have personal events to deal with as well.

I apologize if my constant posting bothers you, but don't tell me that I ask people to do my homework when you're sitting here with no proof, and don't assume just because I post a lot I don't spend a heck of a lot time studying.

More importantly, as an educator, I would tone it done with the "are you serious" nonsense since some kids do have issues.

Just a friendly note of advice, buddy.



Last edited on
closed account (48T7M4Gy)
Now, are you going to help me, or as you'd put it, do my work for me?


The record shows I have helped you many times. It also shows despite your pleadings I, and others, won't do your homework for you.

Bring on your prof, I'm still waiting. A phone number will do.
That's great.

Amazingly--and you'll be real surprised by this--I don't have trouble getting help from most people on here.

In fact, I've never had any trouble getting help from people on here.

Almost everyone has been very nice to me.

God, it's almost as if I don't ask people to do my homework for me, thus why they help in the first place.

His name is Ray Warren.

Where's that proof? That, you know, I'm cheating?
Last edited on
closed account (48T7M4Gy)
phone number?
He only has an office phone at the college for scheduling meetings that he himself doesn't answer because he's an instructor online, therefore it's almost never necessary for us to call him since we all communicate with him through eMessage.

Email him if you want to communicate with him so badly.


Is his name not enough?
Last edited on
closed account (48T7M4Gy)
in my experience, most people who genuinely need reading or processing assistance get it readily and expend a great deal of effort in taking that assistance on board and spending time coming to grips with it. Quick, almost instant, repetitive replies of the type seen here and elsewhere are not in their repertoire.
So, now not only are you making the assumption I tell people to do my work based off of my titles, but now I apparently don't get assistance for my issues and don't spend a lot of time trying to studying based off of YOUR experience with people?

You're amazing. Truly.

You wouldn't know if I get help. You wouldn't know who helps me. You don't know how long I spend studying. You don't know how much progress I've made with my problems, and you still can't provide proof I've asked someone to do my homework for me.

You're not in my life. You don't know me. Don't act like it.

I have a cognitive psychologist and a doctor that help mes me every couple of weeks. They're not computer professors and they're not tutors.

My problems also can't be helped much without surgery.

Therefore, I don't--nor do my parents have the money--have a special cute little full-time teacher that deals with my inability to process words all the time while I code for ten hours a day.

Sorry I don't fit your uh, experience portfolio. I'm stuck getting help on forums and hoping there's some free live tutoring crap online just like any other confused kid.

Last edited on
closed account (48T7M4Gy)
email address will do.
It's just his name @gmail.

Have fun.
Rest assured that we welcome your threads and are eager to help you in any way we can. At least, I am.

And I dont think you were asking us to do your homework. You simply require assistance in assignments, maybe more so than average, but thats okay. Everybody learns at a different pace :)
Now lets slow down for a second, and look at a section of your code. Note that I am only talking about the isPrime function in your code here. This is the most important part of your code, so let focus on this for now. Forget about the rest of your code until we figure this out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isPrime(int number)
{


    int counter = 2;

    while(counter<number)
    {

        if (number%counter==0)

            return false;

        else

            return true;
    }

    return 0;
}


^ this is what you have and its incorrect.

Understand the job of this function, its simple: If 'number' is prime return true, else (if 'number' is not prime) return false.

Before we look at the code, lets try to understand what a prime number is. I'll give a brief explanation here, but you can find load of information about this online.

A prime number is a number that has NO factors besides 1 and itself. This means that a number which cannot be evenly divided by any number less than itself (besides 1, of course, because EVERY number is divisible by 1) is prime.

13 for example is prime. Try dividing 13 by any number from 2 to 12. You will not get a whole number answer for any of those divisions.
12 on the other hand is NOT a prime number, as it is divisible by 6, 4, 3 and 2.

So what the isPrime function must do is take 'number' and divide it by every number from 2 to 'number'-1. As soon as we find an integer that evenly divides into 'number' (i.e. leaves remainder of 0), we know the number is NOT prime, and therefore we return false. All of this will happen inside a loop.

If we've gone through the entire loop without having returned false, that means the number must be prime. So therefore, we should return true OUTSIDE the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool isPrime(int number)
{
    int counter = 2;

    while(counter<number)
    {

        if (number%counter==0)

            return false;

        else

            return true;
    }

    return 0;
}


First of all, get rid of the 'return 0'.

The problem with the above code is that you are only dividing 'number' by 2, and then returning true immediately if it doesn't leave a remainder of 0.

So in summary, there are 2 problems:

(1) We learned that to check if a number is prime, we need to see if it can be divided by ANY number from 0 to 'number'-1. Here, you are only dividing 'number' by 2. You need to increment 'counter' in the loop, so it then checks divisibility by 3, then 4, then 5... and so on.

(2) We learned that we return true outside the while-loop, as that is where we know for sure that the number is prime. You, on the other hand, are potentially returning true as soon as the division by 2 doesn't work.

Check it out. Lets say I pass the number 15 to the isPrime function (spoiler, 15 is not prime!). It should return false, but your function returns true. Why? Because 15 divided by 2 will NOT give a remainder of 0, hence the 'else' clause will be executed, returning true.

The logic here is wrong because we need to check EVERY division from 2 to 14 (i.e. all of them must have a remainder other than 0) before we know for sure that its prime. This is why we return true outside the loop.

So in summary, return false inside loop as soon as remainder is 0. If remainder is not 0, do NOT return true, because we may still have more factors to check. Return true only when we get out of the loop (which is when counter == number), i.e. when all numbers from 2 to 'number'-1 have been checked.

So how will you fix these 2 problems?
Last edited on
Well, thank you for your acceptance. It makes me feel happy.

Why did we remove return 0;?

My idea is making a for loop, but I'm having trouble setting it up.

In a for loop, it will repeat as long as the Boolean expression remains true.

I'd first make a global constant called NUM_FOR_CAL1=2 since we can't have unnamed constants in my class. For simplification sake for now, I'll use a numeric literal for now instead of a named constant.

Then, inside of main I'd declare number.
int num

Display please enter number
Input number

That will be used in the isPrime function.

for(i=2;i<num;num++)

(I'm assuming we don't do i<=num because then we would get the number is not prime since prime numbers are divisible by 1 and themselves as you said)

This will loop until i is more than num where I assume it'll then cut off giving us the number just before the number entered by the user.

After this, inside this loop, for each increment, we can divide number by i to see if when divided it leaves a remainder of 0 since it'll then be being divided by every number except one and itself right before itself.

So this is what I have inside the for loop.

for(i=2;i<num;num++)
if(num%i==0)
return false;
else
return true;

I don't think I need a break statement here since the counts loop controls how many times it loops.

How does this look?



Last edited on
> A prime number can be divided, without a remainder, only by itself and by 1. For example, 17 can be divided only by 1 and by 17

This applies to any prime number. If there is a number that can be divided by another number other than 1 and itself, it is not a prime number. For example, (8) can be divided by 1, 2, 4, 8. Hence (8) is not a prime number.

Ignore other algorithms first, now you obviously know a prime number has only two valid divisors. If there are more than two, then the number is not a prime number. A mere "brute force" method, but better than anything.
1
2
3
4
5
6
7
8
9
10
bool isPrime(int number)
{
    int num_divisors = 0;

    for(int i = 1; i <= number; i++) 
       if((number % i) == 0) num_divisors++;
    
     if (num_divisors == 2) return true;
        else return false;
}
What you're doing kinda makes sense to me, haha.

What was wrong with what I tried to do I should ask?

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
bool isPrime(int number)
{


    int counter = 2;
//like I said before you have to increment the counter in a loop
    while(counter<number)
    {

        if (number%counter==0)

            return false;

        else

return true;//this will just return if the number is not divisible by 2
    }//this would be great for even odd program but not for this





    return 0;//no need for this
}

//also it would be great if you could now rewrite this code and see if you can re-write your own function 


PS: you and kemort should sort out your differences... it would be great... just saying :)
Last edited on
Pages: 123