VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Finally...a code that determines the greatest common factor between two numbers!

by RedSting71 (9 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Sun 30th January 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Finally...a code that determines the greatest common factor between two numbers!

Rate Finally...a code that determines the greatest common factor between two numbers!



--------------------------------------------------------------------------------
Public Function Math_GCF(number1 As Double, number2 As Double) As Double
If number1 > number2 Then a = number2
If number2 > number1 Then a = number1
If number1 = number2 Then a = number1
n1 = number1: n2 = number2
h = n2 / n1
h2 = n1 / n2
If n1 > n2 Then
    If Math_IsInteger(Val(h2)) Then Math_GCF = n2: Exit Function
End If
If n2 > n1 Then
    If Math_IsInteger(Val(h)) Then Math_GCF = n1: Exit Function
End If
y = Math_LCM(number1, number2)
y = Val(y)
x = 1
Do: DoEvents
y = Math_LCM(Val(n1), Val(n2)) / Val(x)
b = n1 / x
c = n2 / x
If Math_IsInteger(Val(y)) Then
    If Math_IsInteger(Val(b)) Then
        If Math_IsInteger(Val(c)) Then
            GCF = x
        End If
    End If
End If
x = x + 1
Loop Until x = a
Math_GCF = GCF
If n1 > n2 Then
    Y1 = Math_LCM(Val(n1), Val(n2))
    Y1 = Val(Y1) / Val(n2)
    b = n1 / n2
    c = n2 / n2
    If Math_IsInteger(Val(Y1)) Then
        If Math_IsInteger(Val(b)) Then
            If Math_IsInteger(Val(c)) Then
                GCF = n2
            End If
        End If
    End If
End If
If n1 < n2 Then
    Y1 = Math_LCM(Val(n1), Val(n2))
    Y1 = Val(Y1) / Val(n1)
    b = n1 / n1
    c = n2 / n1
    If Math_IsInteger(Val(Y1)) Then
        If Math_IsInteger(Val(b)) Then
            If Math_IsInteger(Val(c)) Then
                GCF = n1
            End If
        End If
    End If
End If
Math_GCF = GCF
End Function
--------------------------------------------------------------------------------
Public Function Math_IsInteger(number as double) as double
n1 = Trim(str(number))
n = InStr(1, n1, ".")
If n = 0 Then Math_IsInteger = True
If n > 0 Then Math_IsInteger = False
End Function



Download this snippet    Add to My Saved Code

Finally...a code that determines the greatest common factor between two numbers! Comments

No comments have been posted about Finally...a code that determines the greatest common factor between two numbers!. Why not be the first to post a comment about Finally...a code that determines the greatest common factor between two numbers!.

Post your comment

Subject:
Message:
0/1000 characters