home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / GCD.ICN < prev    next >
Text File  |  1991-07-13  |  732b  |  33 lines

  1. ############################################################################
  2. #
  3. #    Name:    gcd.icn
  4. #
  5. #    Title:    Compute greatest cmmon denominator
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    May 11, 1989
  10. #
  11. ############################################################################
  12. #
  13. #     This procedure computes the greatest common denominator of two
  14. #  integers. If both are zero, it fails.
  15. #
  16. ############################################################################
  17.  
  18. procedure gcd(i,j)
  19.    local r
  20.  
  21.    if i = j = 0 then fail
  22.    if i = 0 then return j
  23.    if j = 0 then return i
  24.    i := abs(i)
  25.    j := abs(j)
  26.    repeat {
  27.       r := i % j
  28.       if r = 0 then return j
  29.       i := j
  30.       j := r
  31.       }
  32. end
  33.