aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2022-07-31 16:35:12 -0500
committerUtil <bruce.gray@acm.org>2022-07-31 16:35:12 -0500
commit591763266da29c3bff937e53ef8eaea7219c4036 (patch)
treedbf79c0f3373780f75be1d4fd210d2bbeb87dd31
parentc549a7288ca66b1b48e8518454806b538fd97568 (diff)
downloadperlweeklychallenge-club-591763266da29c3bff937e53ef8eaea7219c4036.tar.gz
perlweeklychallenge-club-591763266da29c3bff937e53ef8eaea7219c4036.tar.bz2
perlweeklychallenge-club-591763266da29c3bff937e53ef8eaea7219c4036.zip
Add TWC 175 solutions by Bruce Gray : Raku and Perl
-rw-r--r--challenge-175/bruce-gray/perl/ch-1.pl16
-rw-r--r--challenge-175/bruce-gray/perl/ch-2.pl15
-rw-r--r--challenge-175/bruce-gray/raku/ch-1.raku14
-rw-r--r--challenge-175/bruce-gray/raku/ch-2.raku18
4 files changed, 63 insertions, 0 deletions
diff --git a/challenge-175/bruce-gray/perl/ch-1.pl b/challenge-175/bruce-gray/perl/ch-1.pl
new file mode 100644
index 0000000000..12cfb0a7f5
--- /dev/null
+++ b/challenge-175/bruce-gray/perl/ch-1.pl
@@ -0,0 +1,16 @@
+use v5.36;
+use DateTime;
+
+sub last_sundays_in ( $year ) {
+ my @r;
+ for my $month (1..12) {
+ my $dt = DateTime->last_day_of_month( year => $year, month => $month );
+
+ $dt->subtract(days => 1) while $dt->day_name ne 'Sunday';
+
+ push @r, $dt;
+ }
+ return @r;
+}
+
+say $_->ymd for last_sundays_in( shift // 2022 );
diff --git a/challenge-175/bruce-gray/perl/ch-2.pl b/challenge-175/bruce-gray/perl/ch-2.pl
new file mode 100644
index 0000000000..f990879e4d
--- /dev/null
+++ b/challenge-175/bruce-gray/perl/ch-2.pl
@@ -0,0 +1,15 @@
+use v5.36;
+use ntheory qw<euler_phi vecsum>;
+use List::Lazy qw<lazy_range lazy_list>; # Just for fun, to mimic Raku
+
+sub totients_sum ( $n ) {
+ my $totients = lazy_list { $_ > 1 ? $_ = euler_phi $_ : () } $n;
+
+ return vecsum $totients->all;
+}
+
+sub is_perfect_totient () { $_ == totients_sum($_) }
+
+my $perfect_totients = lazy_range(1, undef)->grep(\&is_perfect_totient);
+
+say join ' ', $perfect_totients->next(20);
diff --git a/challenge-175/bruce-gray/raku/ch-1.raku b/challenge-175/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..1fabc8cd00
--- /dev/null
+++ b/challenge-175/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,14 @@
+sub last_sunday_of ( $year, $month ) {
+ my $d = Date.new(:$year, :$month).last-date-in-month;
+
+ return $d - $d.day-of-week % 7;
+}
+sub MAIN ( UInt $year = 2022 ) {
+ say last_sunday_of( $year, $_ ) for 1..12;
+}
+
+# Alternate approach:
+# my @s;
+# @s[.month - 1] = $_ if .day-of-week == 7 for Date.new($year , 1, 1)
+# ..^ Date.new($year+1, 1, 1);
+# .say for @s;
diff --git a/challenge-175/bruce-gray/raku/ch-2.raku b/challenge-175/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..f21b73658e
--- /dev/null
+++ b/challenge-175/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,18 @@
+# Simple version:
+sub totient ( UInt $n --> UInt ) { +grep { $n gcd $_ == 1 }, 1..$n }
+sub totients_sum ( UInt $n --> UInt ) { sum $n, &totient ^… 1 }
+sub perfect_totient ( UInt $n --> Bool ) { $n == $n.&totients_sum }
+
+
+# High-speed version; 1/10th the time of Simple, due to caching and the factor algorithm:
+# sub perfect_totient ( UInt $n --> Bool ) {
+# use Prime::Factor;
+# constant @totients = flat 0, (1..^Inf).map: -> $n {
+# ( $n * [*] $n.&prime-factors.unique.map: 1 - 1/* ).narrow
+# }
+# return $n == [+] $n, { @totients[$^prev] } ^… 1;
+# }
+
+
+constant @perfect_totients = grep &perfect_totient, 1 ..^ Inf;
+say @perfect_totients.head(20);