summaryrefslogtreecommitdiff
path: root/gcd.pl
diff options
context:
space:
mode:
authorIgor <pashev.igor@gmail.com>2011-01-23 14:28:23 +0300
committerIgor <pashev.igor@gmail.com>2011-01-23 14:28:23 +0300
commitb1babe9724561c20bc9a63a60b1bd590f9d5fde8 (patch)
treec13064c4e9c90340ddcc48b362aba1930378bb83 /gcd.pl
parent9e677082cfd5af8d0ac76672784cc00ecaa3ed1a (diff)
downloadgcd-b1babe9724561c20bc9a63a60b1bd590f9d5fde8.tar.gz
Perl
Diffstat (limited to 'gcd.pl')
-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);
+