summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcd.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/gcd.rs b/gcd.rs
new file mode 100644
index 0000000..9e73a44
--- /dev/null
+++ b/gcd.rs
@@ -0,0 +1,27 @@
+use std::env;
+
+fn gcd2(a: u64, b: u64) -> u64
+{
+ let mut a1 = a;
+ let mut b1 = b;
+
+ while b1 != 0 {
+ let c1 = b1;
+ b1 = a1 % b1;
+ a1 = c1;
+ }
+
+ a1
+}
+
+
+fn main ()
+{
+ // XXX skip(1) to skip program name:
+ let nums = env::args().skip(1).map(|s| s.parse().unwrap());
+
+ let gcd = nums.fold(0, |g, n| gcd2(g, n));
+
+ println!("{}", gcd);
+}
+