Results 1 to 7 of 7

GCD - subprogram w/o GCD built in Function

Threaded View

  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:

    Sub HighestFactor()  ' Sub program to calculate the greatest common factor between two numbers
    
    'Declarations
    Dim a As Integer
    Dim b As Integer
    Dim quotient As Single
    
    
    'Inputs
    a = Sheet1.Cells(14, 4).Value
    b = Sheet1.Cells(15, 4).Value
    
    'Process
    If a < 0 And b < 0 Then
        Math.Abs (a)
        Math.Abs (b)
            
            If a > b Then
            'b = divisor
            'a = dividend
            quotient = a / b Mod (a / b)
            a = (b * quotient) Mod (b * quotient)
    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

    For i=1 To 5 Step 1
            Sheet1.Cells(R,C).Value = Number
    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.

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