summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcd.ml33
1 files changed, 33 insertions, 0 deletions
diff --git a/gcd.ml b/gcd.ml
new file mode 100644
index 0000000..85d64d5
--- /dev/null
+++ b/gcd.ml
@@ -0,0 +1,33 @@
+(*
+
+OCaml: http://ocaml.org/
+
+Usage:
+
+ $ ocamlopt gcd.ml -o gcd-ml
+ $ ./gcd-ml 11 22 44 121
+ 11
+
+or:
+
+ $ ocaml gcd.ml 11 22 44 121
+ 11
+
+*)
+
+
+(* GCD of two numbers *)
+let rec gcd a b =
+ match b with
+ | 0 -> a
+ | b -> gcd b (a mod b)
+;;
+
+(* GCD of several numbers *)
+let gcdn = List.fold_left gcd 0 ;;
+
+let args = List.tl (Array.to_list Sys.argv) ;;
+let nums = List.map int_of_string args ;;
+
+Printf.printf "%d\n" (gcdn nums) ;;
+