aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2021-11-02 20:51:57 -0600
committerLuis Mochan <mochan@fis.unam.mx>2021-11-02 20:51:57 -0600
commit9f95ff2232f8433ed009303e8d4ebb21323199d6 (patch)
tree8f6bae081967d38a9e5781bd29365224ce14c480
parentacb047372c5060d4b5c1337a281a1e25c41c9aea (diff)
downloadperlweeklychallenge-club-9f95ff2232f8433ed009303e8d4ebb21323199d6.tar.gz
perlweeklychallenge-club-9f95ff2232f8433ed009303e8d4ebb21323199d6.tar.bz2
perlweeklychallenge-club-9f95ff2232f8433ed009303e8d4ebb21323199d6.zip
Add solution to pwc137
-rw-r--r--challenge-137/wlmb/blog.txt1
-rwxr-xr-xchallenge-137/wlmb/perl/ch-1.pl18
-rwxr-xr-xchallenge-137/wlmb/perl/ch-2.pl27
3 files changed, 46 insertions, 0 deletions
diff --git a/challenge-137/wlmb/blog.txt b/challenge-137/wlmb/blog.txt
new file mode 100644
index 0000000000..3753e2a4c6
--- /dev/null
+++ b/challenge-137/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2021/11/02/PWC137/
diff --git a/challenge-137/wlmb/perl/ch-1.pl b/challenge-137/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..8c7175f66f
--- /dev/null
+++ b/challenge-137/wlmb/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 137
+# Task 1: Long year
+#
+# See https://wlmb.github.io/2021/11/02/PWC137/#task-1-long-year
+use v5.12;
+use warnings;
+use Text::Wrap qw(wrap $columns);
+use integer;
+$columns=62;
+say wrap("", "", grep {is_long_year($_)} ($ARGV[0]//1900..$ARGV[1]//2100));
+sub is_long_year {
+ my $YY=shift;
+ # Skip a day for every 'Gregorian' leap year extrapolating since 0000.
+ my $first_weekday=(($YY-1)+($YY-1)/4-($YY-1)/100+($YY-1)/400)%7; # 0=Monday
+ my $leap=$YY%400==0||$YY%4==0&&$YY%100!=0;
+ return $first_weekday==3 || $leap&&$first_weekday==2;
+}
diff --git a/challenge-137/wlmb/perl/ch-2.pl b/challenge-137/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..c8cb49bbd5
--- /dev/null
+++ b/challenge-137/wlmb/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 137
+# Task 2: Lychrel number
+#
+# See https://wlmb.github.io/2021/11/02/PWC137/#task-2-lychrel-number
+use v5.12;
+use warnings;
+my ($lower, $upper, $maxcount, $largest)=(10,1000,500,10_000_000); #bounds
+foreach(@ARGV){
+ my @why=lychrel($_);
+ say "Input: $_\nOutput: ", @why?"0\n":"1?", @why>1?join("\n", @why):"";
+ say "";
+}
+sub lychrel {
+ my $n=shift;
+ my $r=reverse $n;
+ say("Failed $lower<=$_<=$upper"), exit unless $lower<=$_<=$upper;
+ my $count=$maxcount;
+ my @why=("as");
+ while(--$count){
+ return (@why) if $n eq $r;
+ push @why, "$n+$r=". ($n+=$r);
+ return if $n>=$largest;
+ $r=reverse $n;
+ }
+ return;
+}