aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-175/jo-37/perl/ch-1.pl79
-rwxr-xr-xchallenge-175/jo-37/perl/ch-2.pl73
2 files changed, 152 insertions, 0 deletions
diff --git a/challenge-175/jo-37/perl/ch-1.pl b/challenge-175/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..4c4c998e18
--- /dev/null
+++ b/challenge-175/jo-37/perl/ch-1.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use DateTime::Duration;
+use DateTime::Format::Strptime;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [YEAR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+YEAR
+ Print the date of each month's last Sunday in the given year.
+
+EOS
+
+
+### Input and Output
+
+say for last_sundays(shift);
+
+
+### Implementation
+
+# <blockquote>
+# `perl -ne 'print if 8..13' ../../../challenge-132/dave-jacoby/perl/ch-1.pl`
+# </blockquote>
+
+use constant MONTH =>
+ DateTime::Duration->new(months => 1, end_of_month => 'preserve');
+
+# YYYY-MM-DD
+use constant YMD =>
+ DateTime::Format::Strptime->new(pattern => '%F');
+
+# Returns an array of DateTime objects.
+sub last_sundays ($year) {
+ # Get the end of January.
+ my $jan31 = DateTime->new(
+ year => $year, month => 1, day => 31, formatter => YMD);
+
+ # For each month of the year, get the end day and go back to
+ # the previous Sunday. (day_of_week for Sunday is 7)
+ map $_->subtract(days => $_->day_of_week % 7),
+ map $jan31 + $_ * MONTH, 0 .. 11;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [last_sundays(2022)],
+ [qw(2022-01-30 2022-02-27 2022-03-27 2022-04-24 2022-05-29
+ 2022-06-26 2022-07-31 2022-08-28 2022-09-25 2022-10-30
+ 2022-11-27 2022-12-25)], 'example';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is +(last_sundays(2004))[1]->day_of_month, 29, 'Sun Feb 29 2004';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-175/jo-37/perl/ch-2.pl b/challenge-175/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..6252117098
--- /dev/null
+++ b/challenge-175/jo-37/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Math::Prime::Util qw(euler_phi);
+use List::Gen qw(:iterate :while);
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 1;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N
+ Print the first N perfect totient numbers.
+
+EOS
+
+
+### Input and Output
+
+perfect_totient()->say(shift);
+
+
+### Implementation
+
+# Another exercise in using List::Gen.
+
+sub perfect_totient {
+ # Build a generator for perfect totient numbers. Here we use two
+ # nested generators to accomplish the task.
+
+ # Build a non-caching generator for odd numbers N starting with 3
+ # and filter for perfect totient numbers.
+ iterate_stream {$_ + 2}->from(3)->filter_stream(sub {
+ # Build a generator for the iterative sequence of totients for N
+ # and sum over the sequence's elements. The generated sequence
+ # will start with N because of "from($_)" and will not include 1
+ # according to the chained "until" method. Therefore the
+ # expected sum needs to be adjusted: There is an extra "N" and 1
+ # is missing. Thus we expect a sum of 2 * N - 1 for a perfect
+ # totient number.
+ iterate {euler_phi($_)}->from($_)->until('== 1')->sum == 2 * $_ - 1;
+ });
+}
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is perfect_totient()->take(20), [3, 9, 15, 27, 39, 81, 111, 183,
+ 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375,
+ 5571], 'task 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is perfect_totient()->(25), 46791, 'from A082897';
+ }
+
+ done_testing;
+ exit;
+}