-
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
-
can u pm me some details?i think we can work this out.
-
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.
-
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.
-
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
-
Thanks for the help guys. Its appreciated.
-
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.