summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcd.php28
1 files changed, 28 insertions, 0 deletions
diff --git a/gcd.php b/gcd.php
new file mode 100644
index 0000000..0ab0871
--- /dev/null
+++ b/gcd.php
@@ -0,0 +1,28 @@
+<?php
+
+function gcd2($a, $b)
+{
+ $c = 0;
+ while ($b != 0)
+ {
+ $c = $b;
+ $b = $a % $b;
+ $a = $c;
+ }
+ return $a;
+}
+
+function gcd($numbers)
+{
+ $r = 0;
+ foreach ($numbers as $n)
+ {
+ $r = gcd2($n, $r);
+ }
+ return $r;
+}
+
+print gcd(array_slice($argv, 1));
+
+?>
+