+ Reply to Thread
Results 1 to 7 of 7

GCD - subprogram w/o GCD built in Function

  1. #1
    Registered User
    Join Date
    02-17-2013
    Location
    Stillwater
    MS-Off Ver
    Excel 2003
    Posts
    6

    GCD - subprogram w/o GCD built in Function

    My task is to make a sub program to calculate the greatest common divisor (factor) between two numbers. Here is my code thus far:

    Please Login or Register  to view this content.
    This is where I am getting stuck. I am familiar with the Euclidean Algorithm needed for this problem. However, I am not sure if I am using the Mod function correctly. Also, I do not know how to loop this back for the algorithm to work. I need for the new dividend to be (b*quotient) and for the divisor to be (Mod b*quotient). For extremely large nnumbers, this could get tedious. I need this code to work for any two numbers.

    I only know the basic looping

    Please Login or Register  to view this content.
    etc.

    How do I loop such an algorithm without doing it number by number?
    Last edited by ChemEBA; 03-12-2013 at 03:54 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: GCD - subprogram w/o GCD built in Function

    Another way:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    02-17-2013
    Location
    Stillwater
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: GCD - subprogram w/o GCD built in Function

    wouldn't this be using the built in function? What exactly is the ByVal in front of the variables?
    So, in order for this sub program to work, I would need a separate function program to call back into the sub program?


    Please Login or Register  to view this content.
    Last edited by ChemEBA; 03-12-2013 at 03:49 PM.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: GCD - subprogram w/o GCD built in Function

    wouldn't this be using the built in function?
    No, it's a stand-alone function.

    What exactly is the ByVal in front of the variables?
    It causes the function to receive a copy of the argument, rather than a pointer, which allows it to modify the arguments without modifying the variables passed by the calling routine.

    So, in order for this sub program to work, I would need a separate function program to call back into the sub program?
    It's a function; pass it two numbers, and it returns the GCD. It can be called from a worksheet or other VBA code.

    Please take a few minutes to read the forum rules about CODE tags and quoting entire posts.
    Last edited by shg; 03-10-2013 at 03:03 PM.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: GCD - subprogram w/o GCD built in Function

    So, in order for this sub program to work, I would need a separate function program to call back into the sub program?
    What shg is using is a strategy called recursion http://en.wikipedia.org/wiki/Recursive_algorithm and it is a common strategy in computing -- especially if the "bigger" problem can be broken down into smaller problems that look just like the bigger problem.

    Here's another recursion example for calculating factorials. http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Registered User
    Join Date
    02-17-2013
    Location
    Stillwater
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: GCD - subprogram w/o GCD built in Function

    Thanks for the help, but I ended up going with this code since I had trouble understanding the ByVal thing.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-17-2013
    Location
    Stillwater
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: GCD - subprogram w/o GCD built in Function

    Thanks for the help, but I ended up going with this code since I had trouble understanding the ByVal thing.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1