diff options
| -rw-r--r-- | challenge-125/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-125/abigail/r/ch-1.r | 54 |
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-125/abigail/README.md b/challenge-125/abigail/README.md index edbb6b6ea7..8fb1acbf4d 100644 --- a/challenge-125/abigail/README.md +++ b/challenge-125/abigail/README.md @@ -38,6 +38,7 @@ Output: -1 * [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [R](r/ch-1.r) * [Ruby](ruby/ch-1.rb) ### Blog diff --git a/challenge-125/abigail/r/ch-1.r b/challenge-125/abigail/r/ch-1.r new file mode 100644 index 0000000000..bd64758e3f --- /dev/null +++ b/challenge-125/abigail/r/ch-1.r @@ -0,0 +1,54 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +introot <- function (square) { + return (floor (sqrt (square))) +} + +stdin <- file ('stdin', 'r') +repeat { + n <- readLines (stdin, n = 1) + if (length (n) == 0) { + break + } + n <- as.integer (n) + if (n <= 2) { + cat ("-1\n") + next + } + + n_sq <- n * n + c <- n + 1 + c_sq <- n_sq + 2 * n + 1 + + while (2 * c - 1 <= n_sq) { + b_sq <- c_sq - n_sq + b <- introot (b_sq) + + if (b_sq == b * b) { + cat (n, b, c, "\n") + } + + c_sq <- c_sq + 2 * c + 1 + c <- c + 1 + } + + max_a = floor (n / sqrt (2)) + if (max_a < 3) { + next + } + for (a in 3 : max_a) { + # cat ("a = ", a, "\n") + b_sq <- n_sq - a * a + b <- introot (b_sq) + + if (b_sq == b * b) { + cat (a, b, n, "\n") + } + } +} |
