summaryrefslogtreecommitdiff
path: root/gcd.r
blob: d4e11f95c5d01e679fec970efe261cfad0e70ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env Rscript

# R - https://www.r-project.org/
#
# Usage:
# ./gcd.r 11 22 33 121
# [1] 11
#
# Or:
# Rscript ./gcd.r 11 22 33 121
# [1] 11
#

gcd <- function (a, b) {
  if (b == 0) {
    a
  } else {
    gcd(b, a %% b)
  }
}

gcdn <- function(ns) {
  Reduce(gcd, ns)
}

args <- commandArgs(trailingOnly=TRUE)
ns <- mapply(as.integer, args)
gcdn(ns)