C++ Assignment

1. ## 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

2. can u pm me some details?i think we can work this out.

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 &lt;&lt; "y is a factor of x" &lt;&lt; std::endl;
}
else
{
std::cout &lt;&lt; "y is not a factor of x" &lt;&lt; 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.

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.

5. Code:
```#include &lt;iostream.h&gt;

int main(int argc, char **argv)
{
int number = 0;
int total = 0;

cout &lt;&lt; "Please enter the number you wish to check for factors.\n";
cin &gt;&gt; number;
// we don't care about 0 or 1, nothing divides by 0 and 1 is in every prime
for(int i = 2; i &lt; 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 &lt;&lt; "Number " &lt;&lt; i &lt;&lt; " is a factor of " &lt;&lt; number &lt;&lt; ".\n";
total++;
// total is used to track if the number was prime
}
}

if(! total )
{
cout &lt;&lt; "Number " &lt;&lt; number &lt;&lt; " appears to be prime.\n";
}
else
{
cout &lt;&lt; "Found " &lt;&lt; total &lt;&lt; " 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

6. Thanks for the help guys. Its appreciated.

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 &lt;iostream.h&gt;

int main(int argc, char **argv) {
int number = 0;
int altnum = 0;
int total = 0;

cout &lt;&lt; "Please enter the number you wish to check for factors.\n";
cin &gt;&gt; number;
for (int i = 2; i &lt; (number / 2); i++) {
altnum = number / i;
if (number % i == 0) {
cout &lt;&lt; "Number " &lt;&lt; i &lt;&lt; " is a factor of " &lt;&lt; number &lt;&lt; ".\n";
cout &lt;&lt; "Number " &lt;&lt; altnum &lt;&lt; " is a factor of " &lt;&lt; number &lt;&lt; "\n";
total += 2;
}
}
if (! total) {
cout &lt;&lt; "Number " &lt;&lt; number &lt;&lt; " appears to be prime.\n";
} else {
cout &lt;&lt; "Found " &lt;&lt; total &lt;&lt; " factors.\n";
}
return 0;
}```
Just a simple rework of the code originally posted by nebulus200.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•