Stick Page Forums Archive

C++ question

Started by: doog | Replies: 16 | Views: 1,032

doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 13, 2009 8:30 PM #356736
Not sure if anyone here knows much about C++. I have a question about it though, does anyone know if there is some function that will determine wether or not a number is divided evenly? I'm trying to make one program that determines if a number is prime, and one that determines if a number is a factor of another. I know some ways to do this but was thinking there might be some way to determine simply if a number divides evenly.
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Feb 13, 2009 10:59 PM #356842
The modulus operator (%) sounds like what you want:

7 % 3 = 1 (the remainder of 7/3)
144 % 12 = 0 (there is no remainder, it divides evenly)

So this should work:
if ( num % fac == 0 ) //If there is no remainder, 'fac' must be a factor of 'num'


In the case of checking if a number is prime, you want a remainder for all possible factors.

The method I generally use is to do an initial check to see if the number is even (x % 2 == 0), and if not, then loop from 3 through sqrt(x), incrementing in steps of two to skip even numbers, and seeing if x is evenly divisible by any of those numbers.

There may be a better method online, so I would go Google-ing around. Something like "C++ determining prime numbers" should work.
doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 14, 2009 11:39 AM #357134
Quote from darkcampainger
The modulus operator (%) sounds like what you want:

7 % 3 = 1 (the remainder of 7/3)
144 % 12 = 0 (there is no remainder, it divides evenly)

So this should work:
if ( num % fac == 0 ) //If there is no remainder, 'fac' must be a factor of 'num'


In the case of checking if a number is prime, you want a remainder for all possible factors.

The method I generally use is to do an initial check to see if the number is even (x % 2 == 0), and if not, then loop from 3 through sqrt(x), incrementing in steps of two to skip even numbers, and seeing if x is evenly divisible by any of those numbers.

There may be a better method online, so I would go Google-ing around. Something like "C++ determining prime numbers" should work.


thanks :D

I was thinking I had to do something with the modulus operator but wasn't quite sure in what way.
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Feb 14, 2009 4:03 PM #357190

#include
#include
#include

int main(int argc, char *argv[])
{
int num = 11;
int i = 0;

/* 0 == false */
int isPrime = 0;

/* if it's even */
if((num % 2) != 0)
{
/* while i isn't as big as the sqrt of the number */
for(i = 3; i <= sqrt(num); i++)
{
/* if it can be divided evenly by i, it's not prime (set isPrime to 0) */
if(num % i == 0)
{
isPrime = 0;
/* break so it doesn't set it to prime in the future */
break;
}
/* otherwise set isPrime to 1 because the number is prime */
else
isPrime = 1;
}
}

/* if the number was prime, print it was prime, otherwise, print it wasn't prime */
if(isPrime == 1)
printf("%d is prime\n\n", num);
else
printf("%d isn't prime\n\n", num);

return(0);
}


Thats basically what darkcampaigner was saying but I just thought I would give a working example. It's in C, but you can see how it works (it's easily translatable). math.h contains the sqrt() function, but in C++ it's "cmath". I've commented it so it's easier to understand. A couple of tips:

set i to 3, so that it doesn't divide the number by 0, 1 or 2.
if it finds out it isn't prime make it break from the loop so it doesn't think it's prime in the future.
set isPrime to false, so if it can be divided by 2, and doesn't execute the if statement you don't need to re-initialise it.

I hope this has helped :)

[edit]
It's probably easier on the eyes here: http://rafb.net/p/GhaFok92.html
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Feb 17, 2009 4:48 PM #359178
umm did it help at all?
doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 17, 2009 7:45 PM #359245
Quote from Fluxinator
umm did it help at all?


Yes it did, thanks for the help! :D
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Feb 18, 2009 10:47 PM #359924
Doog, please remove the massive image from your signature, as it stretches out the page:

http://i34.photobucket.com/albums/d136/DooGlaS_/oooooooooopppppppppppppppppp.jpg

A little off-topic, but you have PMing disabled.
doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 18, 2009 11:17 PM #359938
oh, didn't realize I had pming disabled.. thats odd. I'll remove it lol, was just bored yesterday and decided to do that.
doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 19, 2009 9:53 PM #360412
I'm bumping this with another question related to the prime numbers.

I have to make a program that will loop through a set of numbers with a for loop and use another loop inside of it to determine if each number is prime or not and output each prime number. I have it all written but I just keep getting each number as an output. This is what I have at the moment :

#include
using namespace std;
int main()
{
int prime;
for(int number = 3; number <= 100; number ++)
{
for(int divide = 2; divide <= (number - 1); divide ++)
{
prime = number/divide;
if(prime = 0)
{
break;
}

}

}

return 0;
}


I'm just not sure what to do to make it output the number only once and not for every time it divides. Maybe this is because I'm a noob with C++ or because my brain is dead at the moment, but help would be appreciated.
anim44

Posts: 0
Joined: Aug 2025
Feb 19, 2009 10:59 PM #360453
sorry, I program in straight C ;)
doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 19, 2009 11:14 PM #360460
Quote from anim44
sorry, I program in straight C ;)

I think I may have to get a tutor for a class for the first time in my life >.<
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Feb 20, 2009 1:47 AM #360562
Nah, I never learnt from a tutor. I program in straight C and I can recognise the problem. The syntax in that code is virtually the same as it is written in C. You assign 0 to prime instead of comparing them. (i.e. it should be prime == 0, not prime = 0). As 0 is false, and it's another way of putting (prime is false), the statement will never be evaluated. That is equivalent to (if( !prime )). So basically you missed an '='. Also, instead of doing it in 2 steps, why not do:


if( (prime % divide) == 0 )
break;


That says "if prime divided by 'divide' leaves no remainder, break".
The code could be rewritten as so:


#include
using namespace std;

int main( int argc, char *argv[] )
{
int prime = 0, number = 0, divide = 0;
for( number = 3; number <= 100; number++ )
{
for( divide = 2; divide <= (number - 1); divide++ )
{
if( (prime % divide) == 0)
break;
}

}

return 0;
}


I don't see what you're trying to achieve though. This code just loops through each number, and on each number it divides the number by another number in a loop. If the number is not prime it moves on to the next original number. If you tell me what you're trying to do I could maybe make a sample program and try to explain it? Sorry for my explanations, I'm not a very good teacher :P
anim44

Posts: 0
Joined: Aug 2025
Feb 20, 2009 2:37 AM #360597
Quote from doog
I think I may have to get a tutor for a class for the first time in my life >.<


sorry, I didnt get the joke.
doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 22, 2009 6:11 PM #362241
Quote from Fluxinator
Nah, I never learnt from a tutor. I program in straight C and I can recognise the problem. The syntax in that code is virtually the same as it is written in C. You assign 0 to prime instead of comparing them. (i.e. it should be prime == 0, not prime = 0). As 0 is false, and it's another way of putting (prime is false), the statement will never be evaluated. That is equivalent to (if( !prime )). So basically you missed an '='. Also, instead of doing it in 2 steps, why not do:


if( (prime % divide) == 0 )
break;
That says "if prime divided by 'divide' leaves no remainder, break".
The code could be rewritten as so:


#include
using namespace std;

int main( int argc, char *argv[] )
{
int prime = 0, number = 0, divide = 0;
for( number = 3; number <= 100; number++ )
{
for( divide = 2; divide <= (number - 1); divide++ )
{
if( (prime % divide) == 0)
break;
}

}

return 0;
}
I don't see what you're trying to achieve though. This code just loops through each number, and on each number it divides the number by another number in a loop. If the number is not prime it moves on to the next original number. If you tell me what you're trying to do I could maybe make a sample program and try to explain it? Sorry for my explanations, I'm not a very good teacher :P


maybe I should set them as double instead of int, might be messing up the division. This is for a class thats why I need to do it lol, yes it just loops through numbers 3 to 100 outputting each that is prime and thats exactly what I want. But it keeps just outputting all the numbers instead of only the prime ones.. thanks for all your help, I think I may be able to get it though soon. I was pretty tired the day I was working on that lol.
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Feb 23, 2009 6:23 PM #362736
So wait, you need a program to count from 3 to 100, and print if it's prime? If so I can make an example for you.
Website Version: 1.0.4
© 2025 Max Games. All rights reserved.