aboutsummaryrefslogtreecommitdiff
path: root/challenge-175
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-27 21:02:11 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-27 21:02:11 +0100
commit4fcc4e6faca6301608b3777f959573cb0edddffb (patch)
tree0e9ac92059b0e72dd70738a6ee90123ddad4a862 /challenge-175
parentf168a24c6f6e83362aece3e5ff6d483ed749a538 (diff)
downloadperlweeklychallenge-club-4fcc4e6faca6301608b3777f959573cb0edddffb.tar.gz
perlweeklychallenge-club-4fcc4e6faca6301608b3777f959573cb0edddffb.tar.bz2
perlweeklychallenge-club-4fcc4e6faca6301608b3777f959573cb0edddffb.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-175')
-rw-r--r--challenge-175/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-175/laurent-rosenfeld/perl/ch-1.pl23
-rw-r--r--challenge-175/laurent-rosenfeld/perl/ch-2.pl29
-rw-r--r--challenge-175/laurent-rosenfeld/raku/ch-1.raku10
-rw-r--r--challenge-175/laurent-rosenfeld/raku/ch-2.raku16
5 files changed, 79 insertions, 0 deletions
diff --git a/challenge-175/laurent-rosenfeld/blog.txt b/challenge-175/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..238f36a155
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2022/07/perl-weekly-challenge-175-last-sunday-and-perfect-totient-numbers.html
diff --git a/challenge-175/laurent-rosenfeld/perl/ch-1.pl b/challenge-175/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..06c312f3de
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use feature qw/say/;
+use Time::Local;
+
+my $yr = shift // 2022;
+my @months = (0, 31, is_leap($yr) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
+
+for my $month (1..12) {
+ my $month_last_day = timegm( 0, 0, 0, $months[$month], $month - 1, $yr - 1900 );
+ my $day_in_week = (gmtime $month_last_day)[6];
+ my $sunday = $months[$month] - ($day_in_week % 7);
+ printf "%04d/%02d/%02d\n", $yr, $month, $sunday;
+}
+
+sub is_leap {
+ my $yr = shift;
+ return 0 if $yr % 4; # no if not divisible by 4
+ return 1 if $yr % 100; # yes if divisible by 4 but not by 100
+ return 0 if $yr % 400; # no if divisible by 100 and not by 400
+ return 1; # yes if divisibe by 400
+}
diff --git a/challenge-175/laurent-rosenfeld/perl/ch-2.pl b/challenge-175/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..1d649e2371
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,29 @@
+# Unoptimized version, don't use it
+use strict;
+use warnings;
+use feature qw/say/;
+
+sub gcd {
+ my ($i, $j) = sort { $a <=> $b } @_;
+ while ($j) {
+ ($i, $j) = ($j, $i % $j);
+ }
+ return $i;
+}
+sub is_perfect_totient {
+ my $num = shift;
+ my $n = $num;
+ my $sum = 0;
+ while ($n >= 1) {
+ $n = scalar grep { gcd( $n, $_) == 1 } 1..$n-1;
+ $sum += $n;
+ }
+ return $num == $sum;
+}
+my $count = 0;
+my $n = 1;
+while ($count < 20) {
+ print "$n " and $count++ if is_perfect_totient $n;
+ $n++;
+}
+say "";
diff --git a/challenge-175/laurent-rosenfeld/raku/ch-1.raku b/challenge-175/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..7bca863550
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,10 @@
+sub MAIN (Int $yr = 2022) {
+ for ('01'..'09', 10 .. 12).flat -> $month {
+ my $month-end = Date.new("$yr-$month-01").last-date-in-month;
+ my $week_day = $month-end.day-of-week;
+ my $day-in-month = $month-end.day-of-month;
+ # Note: Sunday is weekday 7
+ my $sunday = $day-in-month - ($week_day % 7);
+ say Date.new("$yr-$month-$sunday");
+ }
+}
diff --git a/challenge-175/laurent-rosenfeld/raku/ch-2.raku b/challenge-175/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..7e99ac6ce0
--- /dev/null
+++ b/challenge-175/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,16 @@
+# Unoptimized version, don't use it
+my $count = 0;
+for 2..Inf -> $n {
+ print "$n " and $count++ if is-perfect-totient $n;
+ last if $count >= 20;
+}
+say "";
+sub is-perfect-totient ($num) {
+ my $n = $num;
+ my $sum = 0;
+ while $n >= 1 {
+ $n = (grep { $n gcd $_ == 1 }, 1..^$n).elems;
+ $sum += $n;
+ }
+ return $num == $sum;
+}