aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/abigail/r
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-16 17:42:00 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-16 18:45:01 +0100
commit6a42ef0f1bfd7b33cd271aff9a0323cf97620bad (patch)
treebfb48afd39efcaef25a388e7dfc572de0ffd9a8e /challenge-147/abigail/r
parent4b1c4fc7e1809c724d3d994f0de6d97c4a14b415 (diff)
downloadperlweeklychallenge-club-6a42ef0f1bfd7b33cd271aff9a0323cf97620bad.tar.gz
perlweeklychallenge-club-6a42ef0f1bfd7b33cd271aff9a0323cf97620bad.tar.bz2
perlweeklychallenge-club-6a42ef0f1bfd7b33cd271aff9a0323cf97620bad.zip
Week 147: R solutions
Diffstat (limited to 'challenge-147/abigail/r')
-rw-r--r--challenge-147/abigail/r/ch-1.r46
-rw-r--r--challenge-147/abigail/r/ch-2.r37
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-147/abigail/r/ch-1.r b/challenge-147/abigail/r/ch-1.r
new file mode 100644
index 0000000000..ef6dddc678
--- /dev/null
+++ b/challenge-147/abigail/r/ch-1.r
@@ -0,0 +1,46 @@
+#!/usr/local/bin/Rscript
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+#
+
+#
+# Run as: Rscript ch-1.r
+#
+
+suppressPackageStartupMessages (
+ library (gmp)
+)
+
+todo = c (2, 3, 5, 7)
+cat (todo, "")
+
+count = 20 - length (todo)
+
+pow = 10
+while (count > 0 && length (todo) > 0) {
+ new_todo = c ()
+ for (d in 1 : 9) {
+ for (p in todo) {
+ candidate = d * pow + p
+ if (isprime (candidate) > 0) {
+ cat (candidate, "")
+ count = count - 1
+ new_todo = c (new_todo, candidate)
+ if (count <= 0) {
+ break
+ }
+ }
+ if (count <= 0) {
+ break
+ }
+ }
+ if (count <= 0) {
+ break
+ }
+ }
+ pow = pow * 10
+ todo = new_todo
+}
+
+cat ("\n")
diff --git a/challenge-147/abigail/r/ch-2.r b/challenge-147/abigail/r/ch-2.r
new file mode 100644
index 0000000000..5ae1bf82e5
--- /dev/null
+++ b/challenge-147/abigail/r/ch-2.r
@@ -0,0 +1,37 @@
+#!/usr/local/bin/Rscript
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+#
+
+#
+# Run as: Rscript ch-2.r
+#
+
+suppressPackageStartupMessages (
+ library (hash)
+)
+
+pentagon <- hash ()
+p <- 0
+n <- 0
+done <- FALSE
+
+while (!done) {
+ p <- p + n + n + n + 1
+ n <- n + 1
+ .set (pentagon, p, 1)
+
+ for (seen in keys (pentagon)) {
+ seen <- as.numeric (seen)
+ if (seen + seen <= p &&
+ all (has.key (c (as.character (p - seen),
+ as.character (p - seen - seen)), pentagon))) {
+ cat (seen, p - seen, "\n")
+ done = TRUE
+ }
+ if (done) {
+ break
+ }
+ }
+}