
January 6th, 2004, 03:33 AM
#1
Member
C++ Assignment
Wanted to know if I could get a little help with an assignment.
I have to create a program that can guess the factors of a number.
From what I figure If i can divide X (The number to be factored) by Y evenly then I have figured out the factors. The problem is I have no idea how to test for a whole number. Because I could have a loop increment Y each time until Z is a whole number. Wondered if anyone had an idea of how to test this. I tried a few things, and came up short.
Any help would be appreciated.
Pjd
We all search for a reason to live. It is in that reasoning we discover who we truly are.

January 6th, 2004, 03:40 AM
#2
Member
can u pm me some details?i think we can work this out.

January 6th, 2004, 03:04 PM
#3
If you want to test for a whole number result from a division, use the modulus operator. This returns the remainder of an integer division, so if the remainder is 0 the result will be a whole number.
Code:
int x = 6;
int y = 3;
if ( x % y == 0 )
{
std::cout << "y is a factor of x" << std::endl;
}
else
{
std::cout << "y is not a factor of x" << std::endl;
}
Once you get used to using the modulus operator, writing the program you specify should be a piece of cake. However, you need to do it yourself to understand how everything works, especially if it's an assignment rather than some code you're writing just for fun/learning.

January 6th, 2004, 03:41 PM
#4
Keep in mind when checking for factors that you can keep it to 1/2 or lower than the number, since whatever you end up with as the result of the division can be automatically included. This should speed up your search, since you can ignore higher numbers than A / 2 where A is the number to be factored.
An example of what I'm talking about:
A = 128
B = (A / 2) = 64
:. you know that both 2 and 64 are factors of A. You also know that 64 is the second highest factor of 128, so there is no need to check for anything higher. If you were to do this in a while loop, it would be rather trivial to record both factors as well as limit it to to the search you are looking for.
I've seen a LOT of developers ignore simple mathematical facts like this when writing such pieces of code, and IMO it's bad practice to not apply math properly.
It should be noted that a similar method may be used to calculate primes, for which I developed a small C application.
Chris Shepherd
The NelsonShepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
\"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\"  Failed Insult
Is your whole family retarded, or did they just catch it from you?

January 6th, 2004, 04:17 PM
#5
Code:
#include <iostream.h>
int main(int argc, char **argv)
{
int number = 0;
int total = 0;
cout << "Please enter the number you wish to check for factors.\n";
cin >> number;
// we don't care about 0 or 1, nothing divides by 0 and 1 is in every prime
for(int i = 2; i < number; i++)
{
// % is the mod operator, it divides two numbers and returns the REMAINDER
// if remainder is 0, then we know that number can be divided by i (a factor)
if(number % i == 0)
{
cout << "Number " << i << " is a factor of " << number << ".\n";
total++;
// total is used to track if the number was prime
}
}
if(! total )
{
cout << "Number " << number << " appears to be prime.\n";
}
else
{
cout << "Found " << total << " factors.\n";
}
return 0;
}
Heh, had to keep slapping myself to use C++ instead of perl (every var had a $ in front at first :P ).
/nebulus
There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.
(Merovingian  Matrix Reloaded)

January 6th, 2004, 08:47 PM
#6
Member
Thanks for the help guys. Its appreciated.

January 7th, 2004, 05:50 AM
#7
As an example of the modifications I mentioned above, here is the same thing Nebulus200 did, but with some minor modifications that should result in a net speed gain.
Code:
#include <iostream.h>
int main(int argc, char **argv) {
int number = 0;
int altnum = 0;
int total = 0;
cout << "Please enter the number you wish to check for factors.\n";
cin >> number;
for (int i = 2; i < (number / 2); i++) {
altnum = number / i;
if (number % i == 0) {
cout << "Number " << i << " is a factor of " << number << ".\n";
cout << "Number " << altnum << " is a factor of " << number << "\n";
total += 2;
}
}
if (! total) {
cout << "Number " << number << " appears to be prime.\n";
} else {
cout << "Found " << total << " factors.\n";
}
return 0;
}
Just a simple rework of the code originally posted by nebulus200.
Chris Shepherd
The NelsonShepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
\"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\"  Failed Insult
Is your whole family retarded, or did they just catch it from you?
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

