From 591763266da29c3bff937e53ef8eaea7219c4036 Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 31 Jul 2022 16:35:12 -0500 Subject: Add TWC 175 solutions by Bruce Gray : Raku and Perl --- challenge-175/bruce-gray/perl/ch-1.pl | 16 ++++++++++++++++ challenge-175/bruce-gray/perl/ch-2.pl | 15 +++++++++++++++ challenge-175/bruce-gray/raku/ch-1.raku | 14 ++++++++++++++ challenge-175/bruce-gray/raku/ch-2.raku | 18 ++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 challenge-175/bruce-gray/perl/ch-1.pl create mode 100644 challenge-175/bruce-gray/perl/ch-2.pl create mode 100644 challenge-175/bruce-gray/raku/ch-1.raku create mode 100644 challenge-175/bruce-gray/raku/ch-2.raku 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; +use List::Lazy qw; # 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); -- cgit