summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xgcd.pl21
1 files changed, 21 insertions, 0 deletions
diff --git a/gcd.pl b/gcd.pl
new file mode 100755
index 0000000..550f5f8
--- /dev/null
+++ b/gcd.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+use integer;
+use List::Util qw/ reduce /;
+
+sub gcd2 {
+ my ($a, $b) = @_;
+ $b == 0 ? $a : gcd2($b, $a % $b)
+}
+
+# http://stackoverflow.com/questions/1490505/how-do-i-prevent-listmoreutils-from-warning-about-using-a-and-b-only-once
+sub gcdn {
+ our ($a, $b);
+ reduce {gcd2($a, $b)} $_[0], @_
+}
+
+print gcdn(@ARGV);
+