aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-07-27 11:46:58 +0100
committerGitHub <noreply@github.com>2022-07-27 11:46:58 +0100
commit820e4aaacf46616e84a1240e18200b900b13f8aa (patch)
tree47d422ed411cdd9c1248231adcd07550ae26ad89
parent539fb0c2332a46b66d402420dcc79a9a699d5ad1 (diff)
parentddfd814fdaae67032c56b57043308b237cd75d02 (diff)
downloadperlweeklychallenge-club-820e4aaacf46616e84a1240e18200b900b13f8aa.tar.gz
perlweeklychallenge-club-820e4aaacf46616e84a1240e18200b900b13f8aa.tar.bz2
perlweeklychallenge-club-820e4aaacf46616e84a1240e18200b900b13f8aa.zip
Merge pull request #6512 from choroba/ech175
Solve 175: Last Sunday & Perfect Totient Numbers by E. Choroba
-rwxr-xr-xchallenge-175/e-choroba/perl/ch-1.pl37
-rwxr-xr-xchallenge-175/e-choroba/perl/ch-2.pl29
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-175/e-choroba/perl/ch-1.pl b/challenge-175/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..372dae04e1
--- /dev/null
+++ b/challenge-175/e-choroba/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+use Time::Piece;
+use Time::Seconds qw{ ONE_DAY };
+
+sub last_sunday ($year) {
+ my @dates;
+ for my $month ('01' .. '12') {
+ my $date = 'Time::Piece'->strptime("$year $month 01", '%Y %m %d');
+ my $day = $date->month_last_day - 7;
+ $date += $day * ONE_DAY;
+ $date += ONE_DAY until $date->day eq 'Sun';
+ push @dates, $date->ymd;
+ }
+ return \@dates
+}
+
+use Test2::V0;
+plan 121 * 12 * 3 + 1;
+
+for my $year (1900 .. 2020) {
+ my $dates = last_sunday($year);
+ for my $i (0 .. $#$dates) {
+ my $date = $dates->[$i];
+ my $tp = 'Time::Piece'->strptime($date, '%Y-%m-%d');
+ is $tp->day, 'Sun';
+ is $tp->_mon, $i;
+ cmp_ok($tp->mday + 7, '>', $tp->month_last_day);
+ }
+}
+
+is last_sunday(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 ]];
diff --git a/challenge-175/e-choroba/perl/ch-2.pl b/challenge-175/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..75120e7926
--- /dev/null
+++ b/challenge-175/e-choroba/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use Math::Prime::Util qw{ euler_phi };
+
+sub is_perfect_totient ($x) {
+ my $sum = 0;
+ my $phi = $x;
+ $sum += $phi = euler_phi($phi) while $phi != 1;
+ return $sum == $x
+}
+
+sub perfect_totient_numbers ($size) {
+ my @numbers;
+ my $candidate = 1;
+ while (@numbers < $size) {
+ push @numbers, $candidate if is_perfect_totient($candidate);
+ ++$candidate;
+ }
+ return \@numbers
+}
+
+use Test::More tests => 1;
+
+is_deeply perfect_totient_numbers(20),
+ [3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729,
+ 2187, 2199, 3063, 4359, 4375, 5571];