Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The greatest common divisor of two numbers
#4
Well @Petr I gotta give you credit for your approach: factor both numbers find the factors in common. It would be how I would do it, like Steve did for reducing fractions. So I had to share this way I learned years ago.

Simplifying fractions is just as easy!
Code: (Select All)
_Define A-Z As INTEGER

While 1
    Input "Enter fraction to simplify as n/d, 0 quits "; fraction$
    slash = InStr(1, fraction$, "/")
    n = Val(Mid$(fraction$, 1, slash - 1))
    d = Val(Mid$(fraction$, slash + 1))
    If slash = 0 Or n = 0 Or d = 0 Then End
    g = gcd(n, d): sn = n / g: sd = d / g
    Print n; "/"; d; " simplifies to "; sn; "/"; sd
    Print
Wend

Function gcd (a, b)
    'a and b will be changed unless make copies
    c = a: d = b
    While c <> 0 And d <> 0
        If c > d Then c = c Mod d Else d = d Mod c
    Wend
    gcd = c + d
End Function
b = b + ...
Reply


Messages In This Thread
RE: The greatest common divisor of two numbers - by bplus - 11-05-2024, 04:04 PM



Users browsing this thread: 1 Guest(s)