
October 12th, 2015, 02:37 AM
#1
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;
}

October 12th, 2015, 02:47 AM
#2
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/cpptutorial...sideeffects/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only  not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums  and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.4.0)

October 12th, 2015, 03:03 AM
#3
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.

October 12th, 2015, 03:17 AM
#4
Re: Finding the largest factor
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 builtin types like int and in a separate statement, you will be fine with this understanding.
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.
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.

October 12th, 2015, 07:17 AM
#5
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.

October 12th, 2015, 09:32 AM
#6
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
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

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
