dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Finding the largest factor

  1. #1
    Join Date
    Oct 2015
    Posts
    3

    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;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,918

    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/
    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)

  3. #3
    Join Date
    Oct 2015
    Posts
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,762

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,555

    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.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)