Created
May 4, 2011 16:33
-
-
Save cmcbride/955529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* compute GCD. playing with OCaml *) | |
let rec gcd n m = | |
if m = 0 then | |
n | |
else | |
if n > m | |
then | |
gcd (n-m) m | |
else | |
gcd n (m-n) | |
;; | |
let rec ogcd n m = match n,m with | |
_,0 -> n | |
| n,m when n > m -> ogcd (n-m) m | |
| n,m -> ogcd n (m-n) | |
;; | |
let rec rgcd a b = | |
let r = a mod b in | |
if r = 0 then | |
b | |
else | |
rgcd b r | |
;; | |
let reduce n m = | |
let f = gcd n m in | |
(n/f, m/f) | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment