Finding the largest factor

Hi, i'm a beginner at this. I'm having trouble understanding this program.

This program is to find the largest factor of a number. I don't quite understand how the " factor-- " works. How would i trace this program to better understand it? Thanks in advance!

ex. user enters 24, largest factor = 12

int main() {

int number;

cout << "Enter an integer that is larger than 1: " ;

cin >> number;

if (number <= 1) exit (1);

int factor = number -1;

while ((number % factor) > 0) factor--;

cout << "The largest factor is: " << factor << endl;

return 0;

}

Re: Finding the largest factor

When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.

To trace the program use the debugger to step through the code.

factor-- means that the value is the current value of factor and then 1 is subtracted from factor. In this case the value of factor is not used. See http://www.learncpp.com/cpp-tutorial...-side-effects/

Re: Finding the largest factor

Ahh I see, thanks. I wasn't sure how to post the code.

I'm just a little confused. This is something I'll be tested on and I won't be able use a debugger.

Going by the example: number = 24, largest factor = 12

Code:

`int factor = 24 -1;`

while ((24 % 23) > 0) factor--;

the program prints out 12 as the largest factor.

I'm trying to trace the program by hand. I get stuck because i'm not sure where to go on from there.

Thanks for your help.

Re: Finding the largest factor

Quote:

Originally Posted by **namypo**

I don't quite understand how the " factor-- " works.

As 2kaud explained in word, factor-- as used here is short for:

Code:

`factor = factor - 1;`

You will find out later that there's more to it, but as long as you are using this with the built-in types like int and in a separate statement, you will be fine with this understanding.

Quote:

Originally Posted by **namypo**

How would i trace this program to better understand it?

Try with 6 as the input instead of 24. You will find that the program first tests to see if 5 is a factor, then 4, then 3, and then upon finding that 3 is a factor, declares it by printing.

Quote:

Originally Posted by **namypo**

This program is to find the largest factor of a number.

Incidentally, because the largest factor will have to be multipled with the smallest factor to obtain the number, you may find it more efficient to search for the smallest factor instead, then divide the number by the smallest factor to obtain the largest factor.

Re: Finding the largest factor

The key to that code is really the % operator, which yields the remainder of a division operation. What it's doing is starting with a denominator that is one less than the numerator and performing the division and testing for a remainder. If there's a remainder, it subtracts one and tries again until it finds a denominator that doesn't yield a remainder.

Re: Finding the largest factor

A factor is any number that evenly divides the source value without leaving a remainder.

so 12 is the largest factor of 24 because 24/12 = 2, remainder 0.

other factors of 24 would be 8, 6, 4, 3 and 2.

for even numbers, you'll always get exactly half the input as result for the highest factor. So factor of 186 would be 93, because 186 / 2 (the smallest factor) = 93.

for odd numbers, it will be less than half, and could be as low as 1 if it's a prime number.

for example largest factor of 15 is 5.

largest factor of 17 (prime) is 1. (see below for (*))

while the above routine works, it is not the most efficient way to find the largest factor.

(*) Your routine will return 1 as factor, which is a mathematically debatable answer. For some problems, 1 as factor will be excluded, for others it won't. However if you include 1, you technically should allow the source value itself as being it's own highest factor. But that would make i a somewhat silly program :)