summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2022-11-16 19:26:49 +0200
committerIgor Pashev <pashev.igor@gmail.com>2022-11-16 19:26:49 +0200
commitd39ce2868483a08ef96cecf0e1d8cd95b64042bf (patch)
treeecdca1ef08a97cca5f4831e00a82bee6caea9ddc
parent31585e2402a7f24929b2b85383084725b86affbd (diff)
downloadgcd-d39ce2868483a08ef96cecf0e1d8cd95b64042bf.tar.gz
Add D
-rw-r--r--gcd.d35
1 files changed, 35 insertions, 0 deletions
diff --git a/gcd.d b/gcd.d
new file mode 100644
index 0000000..b9599e4
--- /dev/null
+++ b/gcd.d
@@ -0,0 +1,35 @@
+// D: https://dlang.org
+//
+// Synopsis:
+//
+// GNU D Compiler (tested GCC 12):
+// $ gdc gcd.d -o gcd-d
+// $ ./gcd-d 11 22 33 121
+//
+// LLVM D Compiler (tested LDC2 1.24.0):
+// $ ldc2 --run gcd 11 22 33 121
+// or
+// $ ldc2 --of=gcd-d gcd.d
+// $ ./gcd-d 11 22 33 121
+//
+
+import std.algorithm: map, reduce;
+import std.conv: to;
+import std.stdio: writeln;
+
+ulong gcd2(ulong a, ulong b) {
+ ulong c;
+ while (b > 0) {
+ c = b;
+ b = a % b;
+ a = c;
+ }
+ return c;
+}
+
+void main(string[] args) {
+ if (args.length > 1) {
+ auto gcd = args[1..$].map!(to!ulong).reduce!gcd2();
+ writeln(gcd);
+ }
+}