aboutsummaryrefslogtreecommitdiff
path: root/challenge-147
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-11 01:12:49 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-11 01:12:49 +0100
commit95d45c5753b63b7656a434973453dae12e227a6a (patch)
tree48b99f6c55f64b623ebf98abc3d0814a96c6af67 /challenge-147
parent354215f8375796503461e4cb1d6cde02a566c4cf (diff)
downloadperlweeklychallenge-club-95d45c5753b63b7656a434973453dae12e227a6a.tar.gz
perlweeklychallenge-club-95d45c5753b63b7656a434973453dae12e227a6a.tar.bz2
perlweeklychallenge-club-95d45c5753b63b7656a434973453dae12e227a6a.zip
Week 147: bc solutions
Diffstat (limited to 'challenge-147')
-rw-r--r--challenge-147/abigail/bc/ch-1.bc57
-rw-r--r--challenge-147/abigail/bc/ch-2.bc41
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-147/abigail/bc/ch-1.bc b/challenge-147/abigail/bc/ch-1.bc
new file mode 100644
index 0000000000..52d5cdb0f0
--- /dev/null
+++ b/challenge-147/abigail/bc/ch-1.bc
@@ -0,0 +1,57 @@
+#!/usr/bin/bc
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+#
+
+#
+# Run as: bc ch-1.bc
+#
+
+define is_prime (p) {
+ auto d
+ if (p == 2) {return 1}
+ if (p % 2 == 0) {return 0}
+ for (d = 3; d * d <= p; d += 2) {
+ if (p % d == 0) {return 0}
+ }
+ return 1
+}
+
+todo [1] = 2
+todo [2] = 3
+todo [3] = 5
+todo [4] = 7
+high = 4
+
+for (i = 1; i <= high; i ++) {
+ print todo [i], " "
+}
+
+count = 20 - high
+
+pow = 10
+while (count > 0) {
+ new_high = 0
+ for (d = 1; d <= 9 && count > 0; d ++) {
+ for (i = 1; i <= high && count > 0; i ++) {
+ candidate = d * pow + todo [i]
+ if (is_prime (candidate)) {
+ print candidate, " "
+ new_high = new_high + 1
+ count = count - 1
+ next [new_high] = candidate
+ }
+ }
+ }
+ for (j = 1; j <= new_high; j ++) {
+ todo [j] = next [j]
+ }
+ high = new_high
+ pow = pow * 10
+}
+
+"
+"
+
+halt
diff --git a/challenge-147/abigail/bc/ch-2.bc b/challenge-147/abigail/bc/ch-2.bc
new file mode 100644
index 0000000000..6aee8ec9b4
--- /dev/null
+++ b/challenge-147/abigail/bc/ch-2.bc
@@ -0,0 +1,41 @@
+#!/usr/bin/bc
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+#
+
+#
+# Run as: bc ch-2.bc
+#
+
+define is_pentagonal (p) {
+ auto l, h, m
+ l = l
+ h = high
+ while (l <= h) {
+ m = (l + h) / 2
+ if (pentagon [m] == p) {return 1}
+ if (pentagon [m] < p) {l = m + 1} else {h = m - 1}
+ }
+ return 0
+}
+
+while (!done) {
+ p = p + n + n + n + 1
+ n = n + 1
+ high = high + 1
+
+ pentagon [high] = p
+
+ for (i = 1; i <= high && pentagon [i] + pentagon [i] < p && !done; i ++) {
+ seen = pentagon [i]
+
+ if (is_pentagonal (p - seen) && is_pentagonal (p - seen - seen)) {
+ print seen, " "
+ p - seen
+ done = 1
+ }
+ }
+}
+
+halt