Skip to content

Instantly share code, notes, and snippets.

@cmcbride
Created May 4, 2011 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcbride/955529 to your computer and use it in GitHub Desktop.
Save cmcbride/955529 to your computer and use it in GitHub Desktop.
(* 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