aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-10 15:56:40 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-10 15:56:40 +0100
commitbd82e6dfcf2d047c7916d67e8d931eeec757998a (patch)
treee2a6c2b5a4f57f0f46c36de63382c10e7da51173
parente9411bdc7658179af3f23d3ada7970323547a7d7 (diff)
downloadperlweeklychallenge-club-bd82e6dfcf2d047c7916d67e8d931eeec757998a.tar.gz
perlweeklychallenge-club-bd82e6dfcf2d047c7916d67e8d931eeec757998a.tar.bz2
perlweeklychallenge-club-bd82e6dfcf2d047c7916d67e8d931eeec757998a.zip
Week 147. Perl solutions
-rw-r--r--challenge-147/abigail/perl/ch-1.pl49
-rw-r--r--challenge-147/abigail/perl/ch-2.pl53
-rw-r--r--challenge-147/abigail/t/ctest.ini8
-rw-r--r--challenge-147/abigail/t/input-1-10
-rw-r--r--challenge-147/abigail/t/input-2-10
-rw-r--r--challenge-147/abigail/t/output-1-1.exp1
-rw-r--r--challenge-147/abigail/t/output-2-1.exp1
7 files changed, 112 insertions, 0 deletions
diff --git a/challenge-147/abigail/perl/ch-1.pl b/challenge-147/abigail/perl/ch-1.pl
new file mode 100644
index 0000000000..4e75524897
--- /dev/null
+++ b/challenge-147/abigail/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+#
+
+#
+# Run as: perl ch-1.pl
+#
+
+#
+# This is asking for the first 20 terms of A024785, which we can just
+# grab from https://oeis.org/A024785/list.
+#
+# Or we port the Python code from https://oeis.org/A024785
+#
+
+use Math::Prime::Util qw [is_prime];
+
+my @todo = qw [2 3 5 7];
+my $count = 20;
+
+print "$_ " for @todo;
+$count -= @todo;
+
+MAIN: while (@todo) {
+ my @next;
+ for my $d (1 .. 9) {
+ foreach my $p (@todo) {
+ my $candidate = "$d$p";
+ next unless is_prime ($candidate);
+ print "$candidate ";
+ last MAIN if -- $count <= 0;
+ push @next => $candidate;
+ }
+ }
+ @todo = @next;
+}
+
+print "\n";
diff --git a/challenge-147/abigail/perl/ch-2.pl b/challenge-147/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..e4ed1d17e9
--- /dev/null
+++ b/challenge-147/abigail/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
+#
+
+#
+# Run as: perl ch-2.pl
+#
+
+#
+# We solve this by checking every pentagon number in order to see
+# whether it can be written as a sum of two (smaller) pentagon
+# numbers. If so, if their difference is a pentagon number, we
+# have the winning pair. We'll use a hash which tracks the pentagon
+# numbers seen so far.
+#
+# To calculate the next pentagon number, we will avoid division
+# (and multiplication).
+#
+# If P (N) == N * (3 * N - 1) / 2, then
+#
+# P (N + 1) == (N + 1) * (3 * (N + 1) - 1) / 2
+# == ((N + 1) * (3 * N + 2) / 2
+# == (3 * N * N + 5 * N + 2) / 2
+# == (3 * N * N - N + 6 * N + 2) / 2
+# == (N * (3 * N - 1) / 2) + ((6 * N + 2) / 2)
+# == P (N) + 3 * N + 1
+# == P (N) + N + N + N + 1
+#
+
+my %pentagon;
+
+MAIN: for (;;) {
+ state $n = 0;
+ state $p = 0;
+ $pentagon {$p += $n + $n + $n ++ + 1} ++;
+
+ $_ + $_ < $p && $pentagon {$p - $_}
+ && $pentagon {$p - $_ - $_}
+ && say ("$_ ", $p - $_)
+ && last MAIN for keys %pentagon;
+}
+
diff --git a/challenge-147/abigail/t/ctest.ini b/challenge-147/abigail/t/ctest.ini
new file mode 100644
index 0000000000..bfef483d85
--- /dev/null
+++ b/challenge-147/abigail/t/ctest.ini
@@ -0,0 +1,8 @@
+#
+# Configuration file for running tests, using ctest.
+# See https://github.com/Abigail/Misc/blob/master/ctest
+#
+
+[names]
+1-1 = Fixed Output
+2-1 = Fixed Output
diff --git a/challenge-147/abigail/t/input-1-1 b/challenge-147/abigail/t/input-1-1
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-147/abigail/t/input-1-1
diff --git a/challenge-147/abigail/t/input-2-1 b/challenge-147/abigail/t/input-2-1
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-147/abigail/t/input-2-1
diff --git a/challenge-147/abigail/t/output-1-1.exp b/challenge-147/abigail/t/output-1-1.exp
new file mode 100644
index 0000000000..3648c94b73
--- /dev/null
+++ b/challenge-147/abigail/t/output-1-1.exp
@@ -0,0 +1 @@
+2 3 5 7 13 17 23 37 43 47 53 67 73 83 97 113 137 167 173 197
diff --git a/challenge-147/abigail/t/output-2-1.exp b/challenge-147/abigail/t/output-2-1.exp
new file mode 100644
index 0000000000..de3d6ccd8a
--- /dev/null
+++ b/challenge-147/abigail/t/output-2-1.exp
@@ -0,0 +1 @@
+1560090 7042750